Skip to content Skip to sidebar Skip to footer

Group By Sku, Max Date Sql

I know this is asked quite a bit here, and I have tried to use other examples to incorporate into my own, but I can't seem to make this work. I have columns for sku, date, and cost

Solution 1:

You can add additional logic to get the last date. One method is to add a correlated subquery in the WHERE clause:

SELECT s.PROD_CODE AS Sku, l.REC_DATE AS [Last Date],  li.COST AS Cost
FROM (dbo_LOTS as l INNER JOIN
      dbo_SKU as si
      ON l.SKU_ID = s.SKU_ID
     ) INNER JOIN
     dbo_LOT_ITEM as li
     ON l.LOT_ID = li.LOT_ID
WHERE l.REC_DATE = (SELECT MAX(l2.REC_DATE)
                    FROM dbo_LOTS as l2
                    WHERE l2.SKU_ID = l.SKU_ID
                   );

Solution 2:

This worked:

SELECT dbo_SKU.PROD_CODE AS Sku, dbo_LOTS.REC_DATE AS [Last Date], dbo_LOTS.COSTPERSKU AS Cost
FROM dbo_LOTS INNER JOIN dbo_SKU ON dbo_LOTS.SKU_ID = dbo_SKU.SKU_ID
WHERE (((dbo_LOTS.REC_DATE)=(SELECT MAX(l2.REC_DATE) 

FROM dbo_LOTS as l2 WHERE l2.SKU_ID = dbo_LOTS.SKU_ID)));

Post a Comment for "Group By Sku, Max Date Sql"