嗨,我有一些SQL ......
SELECT
AdviceNo,
Registration
FROM tblSalesDetail
产生类似......
的东西ADV00001, ABC123
ADV00001, CDE564
ADV00002, FGE432
ADV00003, HUY789
ADV00003, MJS532
ADV00003, JFY428
有谁可以告诉我如何调整它以便看到以下内容?
ADV00001, ABC123, 1
ADV00001, CDE564, 2
ADV00002, FGE432, 1
ADV00003, HUY789, 1
ADV00003, MJS532, 2
ADV00003, JFY428, 3
答案 0 :(得分:2)
您可以row_number()
和partition by
使用over
子句根据群组重置计数;语法类似于(注意,未经测试):
SELECT
AdviceNo,
Registration,
row_number() over
(partition by AdviceNo
order by Registration) as Ordinal
FROM tblSalesDetail
partition by
有点像group by
但它不会卷起数据 - 它只会重置您在分区内计算的任何内容。在这种情况下,我们正在计算行号
使用内置函数,并通过组内注册对其进行排序。
This link to the MSDN docs讨论在T-SQL上使用OVER。它在支持它的其他平台上基本相同。
答案 1 :(得分:1)
Oracle分析功能如何?
这是RANK很有用的经典例子; - )
SELECT AdviceNo,
Registration,
RANK () OVER (PARTITION BY AdviceNo ORDER BY Registration ASC) MY_RANK
FROM tblSalesDetail;