尝试按顺序使用 DENSE_RANK() 添加新列
我希望一些例子能帮助解释这种情况。
SELECT
ID,
--ROW_NUMBER() OVER (PARTITION BY CardNumber ORDER BY ID DESC) AS 'RN',
DENSE_RANK() OVER (ORDER BY CardNumber DESC) AS Rank,
CardNumber,
StampNumber,
AuditDate,
FROM [dbo].[XXXX]
ORDER BY ID DESC, AuditDate DESC, StampNumber DESC
我已经阅读了 DENSE_RANK() ,它最接近我正在寻找的内容,但并不完全在那里。 运行这段代码给我
ID | Rank | CardNumber | StampNumber | AuditDate |
---|---|---|---|---|
46 | 1 | 3 | 20 | 2022-03-07 03:45:50.343 |
45 | 1 | 3 | 20 | 2022-03-07 03:45:50.343 |
44 | 2 | 2 | 40 | 2022-03-07 03:45:50.343 |
43 | 2 | 2 | 30 | 2022-03-07 03:45:50.343 |
42 | 2 | 2 | 20 | 2022-03-07 03:45:50.343 |
41 | 2 | 2 | 10 | 2022-03-07 03:45:50.343 |
40 | 3 | 1 | 40 | 2022-03-07 03:45:50.343 |
39 | 3 | 1 | 30 | 2022-03-07 03:45:50.343 |
38 | 3 | 1 | 20 | 2022-03-07 03:45:50.343 |
37 | 3 | 1 | 10 | 2022-03-07 03:45:50.343 |
36 | 1 | 3 | 40 | 2022-03-07 03:45:50.343 |
35 | 1 | 3 | 30 | 2022-03-07 03:45:50.343 |
34 | 1 | 3 | 20 | 2022-03-07 03:45:50.343 |
33 | 1 | 3 | 10 | 2022-03-07 03:45:50.343 |
我正在寻找的结果是
ID | Rank | CardNumber | StampNumber | AuditDate |
---|---|---|---|---|
46 | 1 | 3 | 20 | 2022-03-07 03:45:50.343 |
45 | 1 | 3 | 20 | 2022-03-07 03:45:50.343 |
44 | 2 | 2 | 40 | 2022-03-07 03:45:50.343 |
43 | 2 | 2 | 30 | 2022-03-07 03:45:50.343 |
42 | 2 | 2 | 20 | 2022-03-07 03:45:50.343 |
41 | 2 | 2 | 10 | 2022-03-07 03:45:50.343 |
40 | 3 | 1 | 40 | 2022-03-07 03:45:50.343 |
39 | 3 | 1 | 30 | 2022-03-07 03:45:50.343 |
38 | 3 | 1 | 20 | 2022-03-07 03:45:50.343 |
37 | 3 | 1 | 10 | 2022-03-07 03:45:50.343 |
36 | 4 | 3 | 40 | 2022-03-07 03:45:50.343 |
35 | 4 | 3 | 30 | 2022-03-07 03:45:50.343 |
34 | 4 | 3 | 20 | 2022-03-07 03:45:50.343 |
33 | 4 | 3 | 10 | 2022-03-07 03:45:50.343 |
我希望密集排名仍能对排名进行分组按 CardNumber 但需要排名列按顺序增长重置。 我只想获得前三名。
I'm hoping some examples will help explain the situation.
SELECT
ID,
--ROW_NUMBER() OVER (PARTITION BY CardNumber ORDER BY ID DESC) AS 'RN',
DENSE_RANK() OVER (ORDER BY CardNumber DESC) AS Rank,
CardNumber,
StampNumber,
AuditDate,
FROM [dbo].[XXXX]
ORDER BY ID DESC, AuditDate DESC, StampNumber DESC
I've read up on DENSE_RANK() and it's the closest to what I'm looking for but not quite there.
Running this block of code gives me
ID | Rank | CardNumber | StampNumber | AuditDate |
---|---|---|---|---|
46 | 1 | 3 | 20 | 2022-03-07 03:45:50.343 |
45 | 1 | 3 | 20 | 2022-03-07 03:45:50.343 |
44 | 2 | 2 | 40 | 2022-03-07 03:45:50.343 |
43 | 2 | 2 | 30 | 2022-03-07 03:45:50.343 |
42 | 2 | 2 | 20 | 2022-03-07 03:45:50.343 |
41 | 2 | 2 | 10 | 2022-03-07 03:45:50.343 |
40 | 3 | 1 | 40 | 2022-03-07 03:45:50.343 |
39 | 3 | 1 | 30 | 2022-03-07 03:45:50.343 |
38 | 3 | 1 | 20 | 2022-03-07 03:45:50.343 |
37 | 3 | 1 | 10 | 2022-03-07 03:45:50.343 |
36 | 1 | 3 | 40 | 2022-03-07 03:45:50.343 |
35 | 1 | 3 | 30 | 2022-03-07 03:45:50.343 |
34 | 1 | 3 | 20 | 2022-03-07 03:45:50.343 |
33 | 1 | 3 | 10 | 2022-03-07 03:45:50.343 |
The result I'm looking for is
ID | Rank | CardNumber | StampNumber | AuditDate |
---|---|---|---|---|
46 | 1 | 3 | 20 | 2022-03-07 03:45:50.343 |
45 | 1 | 3 | 20 | 2022-03-07 03:45:50.343 |
44 | 2 | 2 | 40 | 2022-03-07 03:45:50.343 |
43 | 2 | 2 | 30 | 2022-03-07 03:45:50.343 |
42 | 2 | 2 | 20 | 2022-03-07 03:45:50.343 |
41 | 2 | 2 | 10 | 2022-03-07 03:45:50.343 |
40 | 3 | 1 | 40 | 2022-03-07 03:45:50.343 |
39 | 3 | 1 | 30 | 2022-03-07 03:45:50.343 |
38 | 3 | 1 | 20 | 2022-03-07 03:45:50.343 |
37 | 3 | 1 | 10 | 2022-03-07 03:45:50.343 |
36 | 4 | 3 | 40 | 2022-03-07 03:45:50.343 |
35 | 4 | 3 | 30 | 2022-03-07 03:45:50.343 |
34 | 4 | 3 | 20 | 2022-03-07 03:45:50.343 |
33 | 4 | 3 | 10 | 2022-03-07 03:45:50.343 |
I'd like the dense rank to still group the rank by the CardNumber but need the rank column to grow sequentially instead of resetting.
I'm looking to only grab the top 3 ranks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一种间隙与岛屿问题。您正在尝试获取每组相同
CardNumber
值的排名数字(其中无间隙),按ID DESC
排序时。为此,您不能使用
DENSE_RANK
或ROW_NUMBER
,因为它们会将具有相同CardNumber
值的所有行放在一起。有多种解决方案。这是一个:
db<>fiddle
This is a type of gaps-and-islands problem. You are trying to get a ranking number for each group of identical
CardNumber
values (with no gaps), when ordered byID DESC
.You cannot use
DENSE_RANK
orROW_NUMBER
for this, because they will place all rows with the sameCardNumber
value together.There are a number of solutions. Here is one:
db<>fiddle