Help with GROUP BY

  • Hello,

    I'm stuck at my sqlquery.

    What I want is to:

    Group by ID and Location. Sum Quantity and Cost for both Types, Sale and Purchase + Max(date) for an ID where Type = Sale

    Example data: Table OrderData

    ID Date Type Location Quantity Cost

    6 2008-10-02 Sale A 4 3

    6 2008-10-01 Purchase D 88 13

    5 2008-10-13 Sale D 3 10

    6 2008-10-14 Sale D 16 16

    6 2008-10-01 Purchase X 1 13

    6 2008-10-01 Purchase D 22 11

    6 2008-10-01 Purchase C 54 65

    5 2008-10-14 Sale D 3 10

    6 2008-10-31 Sale D 6 12

    4 2008-10-20 Purchase D 5 70

    4 2008-10-05 Purchase D 2 1

    Example of wanted output from exampel data: (Note, ID 4 has no date for type sale)

    ID Location TotalQuantity TotalCost LatestSalesDateForArticleInThatLocation

    6 D 32 72 2008-10-31

    6 A 4 3 2008-10-02

    4 D 7 71

    5 D 3 10 2008-10-14

    I have tried with the query below but then I get lastdate for all types, both purchase and sale and I only want latest date for type sale.

    If I put Where Type = 'Sale' in the where clause in the query I dont get correct totalquantity and totalcoast as it is based on sale and purchase

    SELECT

    ID AS ID,

    Location AS Location,

    SUM(Quantit) AS TotalQuantity,

    SUM(Cost) AS TotalCoast,

    MAX(Date) AS LatestSalesDateForArticleInThatLocation

    FROM

    OrderData

    Group By

    ID, Location

    Regards // Nils

  • Sorry cant give u the whole query...

    Why dont you use a derived query that just selects the max dates and the ID and then join it with the query that you have.

    This would be your derived table query.

    SELECT MAX(Date) ,ID From table

    WHERE Type = 'Sale'

    GROUP BY ID

    "Keep Trying"

  • I'm not familiar with sql derived querys. Can you give me an example?

  • A derived table is sometimes known as an inline view.

    The derived table below has an alias of D:

    SELECT O1.[ID]

    &nbsp&nbsp&nbsp&nbsp,O1.Location

    &nbsp&nbsp&nbsp&nbsp,SUM(O1.Quanity) AS TotalQuantity

    &nbsp&nbsp&nbsp&nbsp,SUM(O1.Cost) AS TotalCost

    &nbsp&nbsp&nbsp&nbsp,D.[Date] AS LatestSalesDate

    FROM OrderData O1

    &nbsp&nbsp&nbsp&nbspLEFT JOIN

    &nbsp&nbsp&nbsp&nbsp(

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspSELECT O2.[ID], O2.Location, MAX(O2.[Date]) AS [Date]

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspFROM OrderData O2

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspWHERE O2.[Type] = 'Sale'

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspGROUP BY O2.[ID], O2.Location

    &nbsp&nbsp&nbsp&nbsp) D

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspON O1.[ID] = D.[ID]

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspAND O1.Location = D.Location

    GROUP BY O1.[ID], O1.Location, D.[Date]

    To answer your original question, you would be better just to use a

    CASE to include only Sales in the LatestSalesDate.

    SELECT [ID]

    &nbsp&nbsp&nbsp&nbsp,Location

    &nbsp&nbsp&nbsp&nbsp,SUM(Quanity) AS TotalQuantity

    &nbsp&nbsp&nbsp&nbsp,SUM(Cost) AS TotalCost

    &nbsp&nbsp&nbsp&nbsp,MAX(CASE WHEN [Type] = 'Sale' THEN [Date] END) AS LatestSalesDate

    FROM OrderData

    GROUP BY [ID], Location

  • check whthr this works

    SELECT

    ID AS ID,

    Location AS Location,

    SUM(Quantit) AS TotalQuantity,

    SUM(Cost) AS TotalCoast,

    D.MaxSaleDate

    FROM

    OrderData O LEFT OUTER JOIN

    (SELECT ID, MAX(DATE) as MaxSaleDate FROM OrderData WHERE type = 'Sale' Group BY ID) D

    ON OrderData.ID = D.ID

    Group By

    ID, Location.

    (SELECT ID, MAX(DATE) as MaxSaleDate FROM OrderData WHERE type = 'Sale' Group BY ID) D is a derived table. i.e. Instead of normally using a table in the from clause we are using this query.

    "Keep Trying"

  • Great!

    Both solutions work but I will go with the case scenario.

    Thanks alot.

    Would it be possible to group quantity and cost for lets say last month? and the month before that?

    ID Location TotalQuantity TotalCost LatestSalesDateForArticleInThatLocation QTYSoldLastMont QTYSold2MontsAgo

    6 D 32 72 2008-10-31

    6 A 4 3 2008-10-02

    4 D 7 71

    5 D 3 10 2008-10-14

  • Sure, just use the same idea. Something like:

    SELECT O.[ID]

    &nbsp&nbsp&nbsp&nbsp,O.Location

    &nbsp&nbsp&nbsp&nbsp,SUM(CASE WHEN O.[Date] >= M.ThisMonth THEN O.Quanity END) AS TotalQuantityThisMonth

    &nbsp&nbsp&nbsp&nbsp,SUM(CASE WHEN O.[Date] >= M.ThisMonth THEN O.Cost END) AS TotalCostThisMonth

    &nbsp&nbsp&nbsp&nbsp,SUM(CASE WHEN O.[Date] >= M.PreviousMonth AND O.[Date] < M.ThisMonth THEN O.Quanity END) AS TotalQuantityPreviousMonth

    &nbsp&nbsp&nbsp&nbsp,SUM(CASE WHEN O.[Date] >= M.PreviousMonth AND O.[Date] < M.ThisMonth THEN O.Cost END) AS TotalCostPreviousMonth

    &nbsp&nbsp&nbsp&nbsp,SUM(CASE WHEN O.[Date] >= M.NextPreviousMonth AND O.[Date] < M.PreviousMonth THEN O.Quanity END) AS TotalQuantityNextPreviousMonth

    &nbsp&nbsp&nbsp&nbsp,SUM(CASE WHEN O.[Date] >= M.NextPreviousMonth AND O.[Date] < M.PreviousMonth THEN O.Cost END) AS TotalCostNextPreviousMonth

    &nbsp&nbsp&nbsp&nbsp,MAX(CASE WHEN O.[Type] = 'Sale' THEN O.[Date] END) AS LatestSalesDate

    FROM OrderData O

    &nbsp&nbsp&nbsp&nbspCROSS JOIN

    &nbsp&nbsp&nbsp&nbsp(

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspSELECT DATEADD(m, DATEDIFF(m, 0, GETDATE()), 0) AS ThisMonth

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp,DATEADD(m, DATEDIFF(m, 0, GETDATE()) - 1, 0) AS PreviousMonth

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp,DATEADD(m, DATEDIFF(m, 0, GETDATE()) - 2, 0) AS NextPreviousMonth

    &nbsp&nbsp&nbsp&nbsp) M

    WHERE O.[Date] >= M.NextPreviousMonth

    GROUP BY O.[ID], O.Location

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply