答案 0 :(得分:0)
试试这个:
SELECT A.ItemName,StockCount
FROM tblStocks A JOIN
(SELECT MAX(stocksID) stocksID
FROM tblStocks
GROUP BY ItemName) B
ON A.stocksID=B.stocksID;
答案 1 :(得分:0)
以下代码对您有所帮助,
select ItemName,StockCount
from tblStocks a
where a.stocksID = (select Max(b.stocksID) from tblStocks b where
a.ItemName = b.ItemName)
答案 2 :(得分:0)
假设StockTest是你的表,请检查以下查询
WITH tmpStock
AS (SELECT ItemName,
MAX(stockId) AS lateststockId
FROM dbo.StockTest
GROUP BY ItemName)
SELECT stockId,
dbo.StockTest.ItemName,
stockcount
FROM tmpStock
INNER JOIN StockTest
ON tmpStock.ItemName = StockTest.ItemName
AND stockId = lateststockId
ORDER BY StockTest.ItemName;