Hi
I am trying to change a fairly complicated query by my standards so it sums 2 fields. I have simplified the query by working on 2 know product codes.
What I am getting in the result is 3 rows where I would expect to get only 1 row per code. For the code p022100WW I am expecting the fields to be like this;
M3onWo = 52.02
Useagedayonwo = 59.29
I am getting 3 rows as per the attached screen shot. I know the code is not devised great but if I could get the rows to sum that would be great. Appreciate any help in advance. thanks
I am trying to change a fairly complicated query by my standards so it sums 2 fields. I have simplified the query by working on 2 know product codes.
What I am getting in the result is 3 rows where I would expect to get only 1 row per code. For the code p022100WW I am expecting the fields to be like this;
M3onWo = 52.02
Useagedayonwo = 59.29
I am getting 3 rows as per the attached screen shot. I know the code is not devised great but if I could get the rows to sum that would be great. Appreciate any help in advance. thanks
SQL:
SELECT ROW_NUMBER() OVER (ORDER BY [W/DaysBeforeZeroStock]) AS [Rank],
dbo.ProductGroup.Name, dbo.ProductGroup.Level1ID,
dbo.[148-vwMinMaxStockLevel_Feed v2 p2].ProductID,
dbo.[148-vwMinMaxStockLevel_Feed v2 p2].ProductCode,
dbo.[148-vwMinMaxStockLevel_Feed v2 p2].Description,
dbo.[148-vwMinMaxStockLevel_Feed v2 p2].StockLevelMin,
dbo.[148-vwMinMaxStockLevel_Feed v2 p2].StockLevelMax,
CASE WHEN StockAvailable <= 0 THEN 0 ELSE StockAvailable END AS StockAvailable,
dbo.[148-vwMinMaxStockLevel_Feed v2 p2].UsagePerDay,
CASE WHEN [W/DaysBeforeZeroStock] <= 0 THEN 0 ELSE [W/DaysBeforeZeroStock] END AS [W/DaysBeforeZeroStock],
SUM((dbo.[148-vwMinMaxStockLevel_Feed v2 p2].[On WO])) AS M3onWO,
dbo.[148-vwMinMaxStockLevel_Feed v2 p2].[Scheduled2],
SUM (dbo.[148-vwMinMaxStockLevel_Feed v2 p2].[Usage Days on Works Order]) AS Useagedayonwo,
dbo.[148-vwMinMaxStockLevel_Feed v2 p2].udfTopTier AS [Top Tier]
FROM dbo.[148-vwMinMaxStockLevel_Feed v2 p2] INNER JOIN
dbo. Product ON dbo.[148-vwMinMaxStockLevel_Feed v2 p2].ProductID = dbo. Product .ProductID INNER JOIN
dbo.ProductGroup ON dbo. Product .ProductGroupID = dbo.ProductGroup.ProductGroupID
WHERE (dbo.ProductGroup.Name NOT LIKE '%mdf%') and (dbo.[148-vwMinMaxStockLevel_Feed v2 p2].ProductCode = 'p022100WW')
or (dbo.[148-vwMinMaxStockLevel_Feed v2 p2].ProductCode = 'g038125RE45')
GROUP BY
dbo.[148-vwMinMaxStockLevel_Feed v2 p2].[ProductID],
dbo.[148-vwMinMaxStockLevel_Feed v2 p2].[ProductCode],
dbo.[148-vwMinMaxStockLevel_Feed v2 p2].[On WO],
dbo.[148-vwMinMaxStockLevel_Feed v2 p2].[Usage Days on Works Order],
dbo.[148-vwMinMaxStockLevel_Feed v2 p2].[W/DaysBeforeZeroStock],
dbo.[ProductGroup].[Name],
dbo.[ProductGroup].[Level1ID],
dbo.[148-vwMinMaxStockLevel_Feed v2 p2].[Description],
dbo.[148-vwMinMaxStockLevel_Feed v2 p2].[StockLevelMin],
dbo.[148-vwMinMaxStockLevel_Feed v2 p2].[StockLevelMax],
dbo.[148-vwMinMaxStockLevel_Feed v2 p2].[StockAvailable],
dbo.[148-vwMinMaxStockLevel_Feed v2 p2].[StockAvailable],
dbo.[148-vwMinMaxStockLevel_Feed v2 p2].[UsagePerDay],
dbo.[148-vwMinMaxStockLevel_Feed v2 p2].[Scheduled2],
dbo.[148-vwMinMaxStockLevel_Feed v2 p2].[udfTopTier]