我想問一個SQL問題(我目前正在使用SQL Server Management Studio)。SQL Server:根據記錄中的其他字段添加參考號
CustomerID ProductID ProductName
------------------------------------------------------
111 6577 ProductA
111 6123 ProductB
111 1133 ProductC
111 1133 ProductC
222 6577 ProductA
222 6577 ProductA
222 1578 ProductL
333 9273 ProductX
和結果,這將取決於在客戶和產品ID添加參考號中的其他列中。(對於相同的客戶ID,如果產品ID是不同的基準數將遞增)
CustomerID ProductID ProductName ref
-----------------------------------------------------------
111 6577 ProductA 111-1
111 6123 ProductB 111-2
111 1133 ProductC 111-3
111 1133 ProductC 111-3
222 6577 ProductA 222-1
222 6577 ProductA 222-1
222 1578 ProductL 222-2
333 9273 ProductX 333-1
我不知道如何通過比較productid來添加參考號?
使用dense_rank()
預先感謝您
dense_rank是正確的。我剛剛編輯 – TriV