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. SQL Query

SQL Query

Scheduled Pinned Locked Moved Database
databasesaleshelp
5 Posts 2 Posters 0 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.
  • W Offline
    W Offline
    Wayne Gaylard
    wrote on last edited by
    #1

    Hi guys, I hope you can help me with this query. Maybe because it is saturday morning I can't seem to work this out. I have 2 tables, Sales and SalesDetails. To simplify things say the Sales table consists of ID, and InvoiceDate, and SalesDetails consists of ID, Quantity, SaleID, ProductID. Now I want to return a list of the total daily sales for a specific month for a specific product, i.e. 01/03/2010 - 5, 02/03/2010 - 10 etc. Hope someone can help

    M 1 Reply Last reply
    0
    • W Wayne Gaylard

      Hi guys, I hope you can help me with this query. Maybe because it is saturday morning I can't seem to work this out. I have 2 tables, Sales and SalesDetails. To simplify things say the Sales table consists of ID, and InvoiceDate, and SalesDetails consists of ID, Quantity, SaleID, ProductID. Now I want to return a list of the total daily sales for a specific month for a specific product, i.e. 01/03/2010 - 5, 02/03/2010 - 10 etc. Hope someone can help

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

      Try this

      SELECT
      SaleDate,
      SUM(Amount)
      FROM SalesDetail SD
      INNER JOIN Sales S ON S.SaleID = SD.SaleID
      WHERE SaleDate BETWEEN ThisDate AND ThatDate
      GROUP BY SaleDate

      Tricky bits will be if you have store time component in your SaleDate, you will need to strip the time off. You can also use the month/year component of the SaleDate instead of the between but it is messier.

      Never underestimate the power of human stupidity RAH

      W 1 Reply Last reply
      0
      • M Mycroft Holmes

        Try this

        SELECT
        SaleDate,
        SUM(Amount)
        FROM SalesDetail SD
        INNER JOIN Sales S ON S.SaleID = SD.SaleID
        WHERE SaleDate BETWEEN ThisDate AND ThatDate
        GROUP BY SaleDate

        Tricky bits will be if you have store time component in your SaleDate, you will need to strip the time off. You can also use the month/year component of the SaleDate instead of the between but it is messier.

        Never underestimate the power of human stupidity RAH

        W Offline
        W Offline
        Wayne Gaylard
        wrote on last edited by
        #3

        It worked. Thank you muchly. The dates are stored with no time component, so that was not an issue. The final SQL came out thus:

        SELECT
        s.InvDate, SUM(sd.Qty) as Amount
        FROM SalesDetails sd
        INNER JOIN Sales s ON s.ID = sd.SaleID
        WHERE s.InvDate BETWEEN DATE(dteStart) AND DATE(dteEnd)
        AND sd.ProdID = ProductID
        GROUP BY s.InvDate;

        M 1 Reply Last reply
        0
        • W Wayne Gaylard

          It worked. Thank you muchly. The dates are stored with no time component, so that was not an issue. The final SQL came out thus:

          SELECT
          s.InvDate, SUM(sd.Qty) as Amount
          FROM SalesDetails sd
          INNER JOIN Sales s ON s.ID = sd.SaleID
          WHERE s.InvDate BETWEEN DATE(dteStart) AND DATE(dteEnd)
          AND sd.ProdID = ProductID
          GROUP BY s.InvDate;

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

          I'm curious, what database are you using as this does not work on SQL Server

          DATE(dteStart)

          Never underestimate the power of human stupidity RAH

          W 1 Reply Last reply
          0
          • M Mycroft Holmes

            I'm curious, what database are you using as this does not work on SQL Server

            DATE(dteStart)

            Never underestimate the power of human stupidity RAH

            W Offline
            W Offline
            Wayne Gaylard
            wrote on last edited by
            #5

            Hi I am using MySql, querying it from VB.Net. deStart and dteEnd are VB variables. MySql does not like VB date variables, and so it is best to convert to date in your SQL Query.

            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