Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. HELP WITH SQL QUERY

HELP WITH SQL QUERY

Scheduled Pinned Locked Moved Database
databasesaleshelp
16 Posts 3 Posters 21 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G GuyThiebaut

    Read up on what the UNION command does. I could tell you what is not working here but you will find out much more if you read, understand than methodically practise what you have read and understood rather than throwing things together hoping they will work then asking for help when they don't work. What the heck, I will tell you anyway - either change ITotal to sTotal or change sTotal to ITotal. The column names for the unioned results need to be the same. As an aside - I don't think that query is going to give you any information that is of any use as you are basically listing sales totals and dates together with invoice totals and dates with no ability to distinguish between what is a sales row and what is an invoice row. I think you need to step away from the computer, read a book, follow tutorials and work out what you want to do on paper before typing SQL queries that you hope will somehow work.

    “That which can be asserted without evidence, can be dismissed without evidence.”

    ― Christopher Hitchens

    B Offline
    B Offline
    BrunoPigeon
    wrote on last edited by
    #7

    You are right, Guy. At this stage I don't fully understand SQL and am attempting queries out of my depth of understanding! Thank you for the encouragement.

    G 2 Replies Last reply
    0
    • B BrunoPigeon

      I appreciate the feedback. This is what is giving me headaches: Select distinct format(SupplierInvoices.Date, 'dd-MMM-yyyy') as tDate, Sum(SupplierInvoices.QTY) as lTotal from SupplierInvoices where PLU = '" & lblPLU.Caption & "' and Date Between #" & StartTime & "# and #" & EndTime & "# Group by format(SupplierInvoices.Date, 'dd-MMM-yyyy') Union Select distinct format(Sales.Date, 'dd-MMM-yyyy') as tDate, Sum(Sales.QTY) as sTotal from Sales where PLU = '" & lblPLU.Caption & "' and Date Between #" & StartTime & "# and #" & EndTime & "# Group by format(Sales.Date, 'dd-MMM-yyyy') It says Item sTotal cannot be found. I am new to SQL and am trying to learn, hence asking on this forum.

      M Offline
      M Offline
      Mycroft Holmes
      wrote on last edited by
      #8

      BrunoPigeon wrote:

      I am new to SQL and am trying to learn, hence asking on this forum.

      Ok there is something REALLY wrong with this. You are trying to learn SQL using the absolutely WRONG tools. As I said VB6 is dead, you will not get anything but bad memories from old farts who used to use it. Access is not a suitable learning platform for SQL it has some weird stuff unique to Access. Port your database to SQL Server (there is a clear migration path) and use the proper tools to learn TSQL. You are doing yourself a disservice continuing to use these tools as you will need to UNLEARN a lot of stuff. Besides there are a huge number of resources to help you with the current tool sets.

      Never underestimate the power of human stupidity - RAH I'm old. I know stuff - JSOP

      G B 2 Replies Last reply
      0
      • B BrunoPigeon

        I appreciate the feedback. This is what is giving me headaches: Select distinct format(SupplierInvoices.Date, 'dd-MMM-yyyy') as tDate, Sum(SupplierInvoices.QTY) as lTotal from SupplierInvoices where PLU = '" & lblPLU.Caption & "' and Date Between #" & StartTime & "# and #" & EndTime & "# Group by format(SupplierInvoices.Date, 'dd-MMM-yyyy') Union Select distinct format(Sales.Date, 'dd-MMM-yyyy') as tDate, Sum(Sales.QTY) as sTotal from Sales where PLU = '" & lblPLU.Caption & "' and Date Between #" & StartTime & "# and #" & EndTime & "# Group by format(Sales.Date, 'dd-MMM-yyyy') It says Item sTotal cannot be found. I am new to SQL and am trying to learn, hence asking on this forum.

        G Offline
        G Offline
        GuyThiebaut
        wrote on last edited by
        #9

        Have a look at my other comment where I explain what is going on. I'd also suggest that you get into the habit of formatting your SQL so that is is easier to read. Here's an example of a possible way of formatting your SQL:

        Select
        distinct format(SupplierInvoices.Date, 'dd-MMM-yyyy') as tDate,
        Sum(SupplierInvoices.QTY) as lTotal
        from SupplierInvoices
        where PLU = '" & lblPLU.Caption & "'
        and Date Between #" & StartTime & "# and #" & EndTime & "#
        Group by format(SupplierInvoices.Date, 'dd-MMM-yyyy')
        Union
        Select
        distinct format(Sales.Date, 'dd-MMM-yyyy') as tDate,
        Sum(Sales.QTY) as sTotal
        from Sales
        where PLU = '" & lblPLU.Caption & "'
        and Date Between #" & StartTime & "# and #" & EndTime & "#
        Group by format(Sales.Date, 'dd-MMM-yyyy')

        “That which can be asserted without evidence, can be dismissed without evidence.”

        ― Christopher Hitchens

        B 1 Reply Last reply
        0
        • B BrunoPigeon

          You are right, Guy. At this stage I don't fully understand SQL and am attempting queries out of my depth of understanding! Thank you for the encouragement.

          G Offline
          G Offline
          GuyThiebaut
          wrote on last edited by
          #10

          Take it slowly and learn from the beginning - it's the classic thing of if you bite off more than you can at first chew you will be put off by it. If you are diligent, within a couple of weeks you will be able to solve these sorts of issues without too much help. Good luck!

          “That which can be asserted without evidence, can be dismissed without evidence.”

          ― Christopher Hitchens

          1 Reply Last reply
          0
          • M Mycroft Holmes

            BrunoPigeon wrote:

            I am new to SQL and am trying to learn, hence asking on this forum.

            Ok there is something REALLY wrong with this. You are trying to learn SQL using the absolutely WRONG tools. As I said VB6 is dead, you will not get anything but bad memories from old farts who used to use it. Access is not a suitable learning platform for SQL it has some weird stuff unique to Access. Port your database to SQL Server (there is a clear migration path) and use the proper tools to learn TSQL. You are doing yourself a disservice continuing to use these tools as you will need to UNLEARN a lot of stuff. Besides there are a huge number of resources to help you with the current tool sets.

            Never underestimate the power of human stupidity - RAH I'm old. I know stuff - JSOP

            G Offline
            G Offline
            GuyThiebaut
            wrote on last edited by
            #11

            :thumbsup: Excellent points. I learnt SQL using Access and basically I learnt nothing of use. Then when I picked up SQL Server and did the training courses, the world of SQL open up to me with its resplendent unicorn rainbows and... okay I am exaggerating a bit but I think you make some very good points in your comment.

            “That which can be asserted without evidence, can be dismissed without evidence.”

            ― Christopher Hitchens

            1 Reply Last reply
            0
            • B BrunoPigeon

              You are right, Guy. At this stage I don't fully understand SQL and am attempting queries out of my depth of understanding! Thank you for the encouragement.

              G Offline
              G Offline
              GuyThiebaut
              wrote on last edited by
              #12

              Maybe try something like this: SQL in 24 Hours, Sams Teach Yourself: Amazon.co.uk: Ryan Stephens, Arie D. Jones, Ron Plew: 9780672337598: Books[[^](https://www.amazon.co.uk/SQL-Hours-Sams-Teach-Yourself/dp/0672337592/ref=sr_1_1?
              crid=2DRD3GTNWLWQD&keywords=sql+in+24+hours&qid=1554450027&s=gateway&sprefix=sql+in+24%2Caps%2C139&sr=8-1 "New Window")] The books can be a bit expensive but I have generally found that the series is a good introduction to any topic and you can usually get through all the exercises in a week.

              “That which can be asserted without evidence, can be dismissed without evidence.”

              ― Christopher Hitchens

              1 Reply Last reply
              0
              • G GuyThiebaut

                Have a look at my other comment where I explain what is going on. I'd also suggest that you get into the habit of formatting your SQL so that is is easier to read. Here's an example of a possible way of formatting your SQL:

                Select
                distinct format(SupplierInvoices.Date, 'dd-MMM-yyyy') as tDate,
                Sum(SupplierInvoices.QTY) as lTotal
                from SupplierInvoices
                where PLU = '" & lblPLU.Caption & "'
                and Date Between #" & StartTime & "# and #" & EndTime & "#
                Group by format(SupplierInvoices.Date, 'dd-MMM-yyyy')
                Union
                Select
                distinct format(Sales.Date, 'dd-MMM-yyyy') as tDate,
                Sum(Sales.QTY) as sTotal
                from Sales
                where PLU = '" & lblPLU.Caption & "'
                and Date Between #" & StartTime & "# and #" & EndTime & "#
                Group by format(Sales.Date, 'dd-MMM-yyyy')

                “That which can be asserted without evidence, can be dismissed without evidence.”

                ― Christopher Hitchens

                B Offline
                B Offline
                BrunoPigeon
                wrote on last edited by
                #13

                I really appreciate all the things you said, Guy. Thanks a ton. I will dive into this this weekend and hopefully by Sunday, have a solution to my own question! Take care & thanks for your valuable input. Bruno

                G 1 Reply Last reply
                0
                • M Mycroft Holmes

                  BrunoPigeon wrote:

                  I am new to SQL and am trying to learn, hence asking on this forum.

                  Ok there is something REALLY wrong with this. You are trying to learn SQL using the absolutely WRONG tools. As I said VB6 is dead, you will not get anything but bad memories from old farts who used to use it. Access is not a suitable learning platform for SQL it has some weird stuff unique to Access. Port your database to SQL Server (there is a clear migration path) and use the proper tools to learn TSQL. You are doing yourself a disservice continuing to use these tools as you will need to UNLEARN a lot of stuff. Besides there are a huge number of resources to help you with the current tool sets.

                  Never underestimate the power of human stupidity - RAH I'm old. I know stuff - JSOP

                  B Offline
                  B Offline
                  BrunoPigeon
                  wrote on last edited by
                  #14

                  I know MyCroft! I am busy porting it over to MySQL but , GEEEEZ, alot must be changed in the code! But yes, I agree vb6 is dead. It's just that I have an old program I still support. So hanging in there on the last threads of vb6!! Thank you for your input - I really appreciate it. Bruno

                  1 Reply Last reply
                  0
                  • B BrunoPigeon

                    I really appreciate all the things you said, Guy. Thanks a ton. I will dive into this this weekend and hopefully by Sunday, have a solution to my own question! Take care & thanks for your valuable input. Bruno

                    G Offline
                    G Offline
                    GuyThiebaut
                    wrote on last edited by
                    #15

                    You're welcome. You will get there - good on you for bearing with things!

                    “That which can be asserted without evidence, can be dismissed without evidence.”

                    ― Christopher Hitchens

                    B 1 Reply Last reply
                    0
                    • G GuyThiebaut

                      You're welcome. You will get there - good on you for bearing with things!

                      “That which can be asserted without evidence, can be dismissed without evidence.”

                      ― Christopher Hitchens

                      B Offline
                      B Offline
                      BrunoPigeon
                      wrote on last edited by
                      #16

                      Cheers, brother! I will. You will be the first to know.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups