不完全知道如何我會形容我想要的,但我有一個具有以下值的表:SQL Server - 如何檢索表中每行的每個值的遞增計數?
ID GoodsNumber
1 700
2 701
3 700
4 700
5 701
我怎樣才能檢索到的ID號(及其關聯GoodsNumbers),具有計數沿該GoodsNumber(由ID有序),將一次出現如此如下:
ID GoodsNumber Count
1 700 1
2 701 1
3 700 2
4 700 3
5 701 2
我試過以下,但它不工作 - 它只是給我上的每一行的總數:
SELECT A.ID, A.GoodsNum, B.Count
FROM Tbl A
INNER JOIN
(SELECT GoodsNum, count(GoodsNum) as Count
FROM Tbl
GROUP BY GoodsNum) B
ON A.GoodsNum = B.GoodsNum
希望這有助於:SELECT *,ROW_NUMBER()過(按GoodNumber ORDER BY ID劃分)AS數 FROM Goods ORDER BY ID – Rajesh