tonyvee1973
IS-IT--Management
Hi Guys
Having a headache with this one and really could do with some guidance.
I have the script below:
SELECT dbo.STK_STOCK.STKCODE, '' AS Spare, dbo.STK_STOCK.STKNAME, '' AS UnitOfMeasure, '' AS UnitOfSales, dbo.STK_STOCK.STK_BASEPRICE,
dbo.STK_STOCK3.STK_USRNUM1 AS RRP, '' AS PriceCode,
SUM(dbo.STK_LOCATION.LOC_PHYSICAL - dbo.STK_LOCATION.LOC_RESERVE_OUT) AS STK_PHYSICAL,
dbo.POP_DETAIL.POD_QTYRESERVED AS QuantityDue, MIN(POP_DETAIL.POD_REQDATE) AS NextDelivery, '' AS ColourCode, '' AS ProductGroup,
(CASE dbo.STK_STOCK.STK_DO_NOT_USE when 1 then 'Y' ELSE 'N' END) as Discontinued,
(CASE dbo.STK_STOCK3.STK_USRFLAG1 WHEN 1 then 'Y' ELSE 'N' END) as Dead,
dbo.STK_STOCK.STK_BARCODE AS BarCode, '' AS MinimumMultiples, '' AS GenericCode5,
'' AS GenericCode6, dbo.STK_STOCK.STK_SORT_KEY, dbo.STK_STOCK.STK_SORT_KEY1, '' AS FreeType3, '' AS FreeType4, '' AS FreeType5, '' AS FreeType6, '' AS FreeType7,
'' AS FreeType8, '' AS BreakCode, ROUND(dbo.STK_STOCK.STK_COSTPRICE / dbo.STK_STOCK_2.STK_BUY_UNIT1, 2) AS STK_COSTPRICE, '' AS MinimumPrice,
'' AS RangeCode, '' AS MinimumPromotional, '' AS TaxCode, '' AS MinimumOrderQuantity, '' AS FutureStock
FROM dbo.STK_STOCK INNER JOIN
dbo.STK_STOCK_2 ON dbo.STK_STOCK.STKCODE = dbo.STK_STOCK_2.STKCODE2 INNER JOIN
dbo.STK_STOCK3 ON dbo.STK_STOCK_2.STKCODE2 = dbo.STK_STOCK3.STKCODE3 INNER JOIN
dbo.STK_LOCATION ON STK_STOCK3.STKCODE3 = dbo.STK_LOCATION.LOC_STOCK_CODE INNER JOIN
dbo.POP_DETAIL ON STK_STOCK3.STKCODE3 = dbo.POP_DETAIL.POD_STOCK_CODE
WHERE (dbo.STK_STOCK3.STK_USRFLAG1 <> 1) AND (dbo.STK_LOCATION.LOC_CODE) NOT IN ('CREDIT','RETURN','REJECT') and (POP_DETAIL.POD_QTYRESERVED <>0) and (dbo.STK_STOCK.STKCODE = '11111')
GROUP BY dbo.STK_STOCK.STKCODE, dbo.STK_STOCK.STKNAME, dbo.STK_STOCK.STK_BASEPRICE, dbo.STK_STOCK3.STK_USRNUM1,dbo.POP_DETAIL.POD_QTYRESERVED, POP_DETAIL.POD_REQDATE, dbo.STK_STOCK.STK_DO_NOT_USE,
dbo.STK_STOCK3.STK_USRFLAG1,dbo.STK_STOCK.STK_BARCODE, dbo.STK_STOCK.STK_SORT_KEY, dbo.STK_STOCK.STK_SORT_KEY1, dbo.STK_STOCK.STK_COSTPRICE / dbo.STK_STOCK_2.STK_BUY_UNIT1
Originally, the script returned two lines for the stk code 11111 showing two different dates for the "POP_DETAIL.POD_REQDATE" field - i only wanted the line with the lowest date so added the MIN (Above in bold within the script)
I think that i have gone wrong somewhere as it seems to ignore the MIN and still returns two lines - I have tried several ways but its driving me nuts!
Any help really would be appreciated
Thanks as always
Having a headache with this one and really could do with some guidance.
I have the script below:
SELECT dbo.STK_STOCK.STKCODE, '' AS Spare, dbo.STK_STOCK.STKNAME, '' AS UnitOfMeasure, '' AS UnitOfSales, dbo.STK_STOCK.STK_BASEPRICE,
dbo.STK_STOCK3.STK_USRNUM1 AS RRP, '' AS PriceCode,
SUM(dbo.STK_LOCATION.LOC_PHYSICAL - dbo.STK_LOCATION.LOC_RESERVE_OUT) AS STK_PHYSICAL,
dbo.POP_DETAIL.POD_QTYRESERVED AS QuantityDue, MIN(POP_DETAIL.POD_REQDATE) AS NextDelivery, '' AS ColourCode, '' AS ProductGroup,
(CASE dbo.STK_STOCK.STK_DO_NOT_USE when 1 then 'Y' ELSE 'N' END) as Discontinued,
(CASE dbo.STK_STOCK3.STK_USRFLAG1 WHEN 1 then 'Y' ELSE 'N' END) as Dead,
dbo.STK_STOCK.STK_BARCODE AS BarCode, '' AS MinimumMultiples, '' AS GenericCode5,
'' AS GenericCode6, dbo.STK_STOCK.STK_SORT_KEY, dbo.STK_STOCK.STK_SORT_KEY1, '' AS FreeType3, '' AS FreeType4, '' AS FreeType5, '' AS FreeType6, '' AS FreeType7,
'' AS FreeType8, '' AS BreakCode, ROUND(dbo.STK_STOCK.STK_COSTPRICE / dbo.STK_STOCK_2.STK_BUY_UNIT1, 2) AS STK_COSTPRICE, '' AS MinimumPrice,
'' AS RangeCode, '' AS MinimumPromotional, '' AS TaxCode, '' AS MinimumOrderQuantity, '' AS FutureStock
FROM dbo.STK_STOCK INNER JOIN
dbo.STK_STOCK_2 ON dbo.STK_STOCK.STKCODE = dbo.STK_STOCK_2.STKCODE2 INNER JOIN
dbo.STK_STOCK3 ON dbo.STK_STOCK_2.STKCODE2 = dbo.STK_STOCK3.STKCODE3 INNER JOIN
dbo.STK_LOCATION ON STK_STOCK3.STKCODE3 = dbo.STK_LOCATION.LOC_STOCK_CODE INNER JOIN
dbo.POP_DETAIL ON STK_STOCK3.STKCODE3 = dbo.POP_DETAIL.POD_STOCK_CODE
WHERE (dbo.STK_STOCK3.STK_USRFLAG1 <> 1) AND (dbo.STK_LOCATION.LOC_CODE) NOT IN ('CREDIT','RETURN','REJECT') and (POP_DETAIL.POD_QTYRESERVED <>0) and (dbo.STK_STOCK.STKCODE = '11111')
GROUP BY dbo.STK_STOCK.STKCODE, dbo.STK_STOCK.STKNAME, dbo.STK_STOCK.STK_BASEPRICE, dbo.STK_STOCK3.STK_USRNUM1,dbo.POP_DETAIL.POD_QTYRESERVED, POP_DETAIL.POD_REQDATE, dbo.STK_STOCK.STK_DO_NOT_USE,
dbo.STK_STOCK3.STK_USRFLAG1,dbo.STK_STOCK.STK_BARCODE, dbo.STK_STOCK.STK_SORT_KEY, dbo.STK_STOCK.STK_SORT_KEY1, dbo.STK_STOCK.STK_COSTPRICE / dbo.STK_STOCK_2.STK_BUY_UNIT1
Originally, the script returned two lines for the stk code 11111 showing two different dates for the "POP_DETAIL.POD_REQDATE" field - i only wanted the line with the lowest date so added the MIN (Above in bold within the script)
I think that i have gone wrong somewhere as it seems to ignore the MIN and still returns two lines - I have tried several ways but its driving me nuts!
Any help really would be appreciated
Thanks as always