如何显示具有最新数量的项目

时间:2018-02-22 05:28:47

标签: c# sql

这是tblStocks:

This is tblStocks

什么SQL查询只显示项目的最新计数?

预期输出:

result should display this

3 个答案:

答案 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;