这是我的疑问:
SELECT ITM.CODE
, Price
, STL.DATE_ AS gun
FROM LG_094_ITEMS ITM WITH (NOLOCK)
INNER JOIN LG_094_01_STLINE STL ON ITM.LOGICALREF = STL.STOCKREF
WHERE Price > 0
AND TRCODE = 1
AND CANCELLED = 0
AND INVOICEREF > 0
AND STL.LINETYPE = 0
我无法知道如何获取代码的最新价格。
感谢您的帮助。
答案 0 :(得分:3)
您可以在此处使用ROW_NUMBER()
:
SELECT *
FROM (
SELECT ITM.CODE
, Price
, STL.DATE_ AS gun
, ROW_NUMBER() OVER (PARTITION BY ITM.CODE ORDER BY STL.DATE_ DESC) AS RN
FROM LG_094_ITEMS ITM WITH (NOLOCK)
INNER JOIN LG_094_01_STLINE STL ON ITM.LOGICALREF = STL.STOCKREF
WHERE Price > 0
AND TRCODE = 1
AND CANCELLED = 0
AND INVOICEREF > 0
AND STL.LINETYPE = 0
) prices
WHERE RN = 1
答案 1 :(得分:2)
SELECT *
FROM (
SELECT ITM.CODE
, Price
, STL.DATE_ AS gun
, RowNum = ROW_NUMBER() OVER (PARTITION BY ITM.CODE ORDER BY STL.DATE_ DESC)
FROM LG_094_ITEMS ITM
INNER JOIN LG_094_01_STLINE STL ON ITM.LOGICALREF = STL.STOCKREF
WHERE Price > 0
AND TRCODE = 1
AND CANCELLED = 0
AND INVOICEREF > 0
AND STL.LINETYPE = 0
) t
WHERE t.RowNum = 1