尝试按顺序使用 DENSE_RANK() 添加新列

发布于 2025-01-12 09:09:37 字数 3454 浏览 0 评论 0原文

我希望一些例子能帮助解释这种情况。

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() ,它最接近我正在寻找的内容,但并不完全在那里。 运行这段代码给我

IDRankCardNumberStampNumberAuditDate
4613202022-03-07 03:45:50.343
4513202022-03-07 03:45:50.343
4422402022-03-07 03:45:50.343
4322302022-03-07 03:45:50.343
4222202022-03-07 03:45:50.343
4122102022-03-07 03:45:50.343
4031402022-03-07 03:45:50.343
3931302022-03-07 03:45:50.343
3831202022-03-07 03:45:50.343
3731102022-03-07 03:45:50.343
3613402022-03-07 03:45:50.343
3513302022-03-07 03:45:50.343
3413202022-03-07 03:45:50.343
3313102022-03-07 03:45:50.343

我正在寻找的结果是

IDRankCardNumberStampNumberAuditDate
4613202022-03-07 03:45:50.343
4513202022-03-07 03:45:50.343
4422402022-03-07 03:45:50.343
4322302022-03-07 03:45:50.343
4222202022-03-07 03:45:50.343
4122102022-03-07 03:45:50.343
4031402022-03-07 03:45:50.343
3931302022-03-07 03:45:50.343
3831202022-03-07 03:45:50.343
3731102022-03-07 03:45:50.343
3643402022-03-07 03:45:50.343
3543302022-03-07 03:45:50.343
3443202022-03-07 03:45:50.343
3343102022-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

IDRankCardNumberStampNumberAuditDate
4613202022-03-07 03:45:50.343
4513202022-03-07 03:45:50.343
4422402022-03-07 03:45:50.343
4322302022-03-07 03:45:50.343
4222202022-03-07 03:45:50.343
4122102022-03-07 03:45:50.343
4031402022-03-07 03:45:50.343
3931302022-03-07 03:45:50.343
3831202022-03-07 03:45:50.343
3731102022-03-07 03:45:50.343
3613402022-03-07 03:45:50.343
3513302022-03-07 03:45:50.343
3413202022-03-07 03:45:50.343
3313102022-03-07 03:45:50.343

The result I'm looking for is

IDRankCardNumberStampNumberAuditDate
4613202022-03-07 03:45:50.343
4513202022-03-07 03:45:50.343
4422402022-03-07 03:45:50.343
4322302022-03-07 03:45:50.343
4222202022-03-07 03:45:50.343
4122102022-03-07 03:45:50.343
4031402022-03-07 03:45:50.343
3931302022-03-07 03:45:50.343
3831202022-03-07 03:45:50.343
3731102022-03-07 03:45:50.343
3643402022-03-07 03:45:50.343
3543302022-03-07 03:45:50.343
3443202022-03-07 03:45:50.343
3343102022-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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

┈┾☆殇 2025-01-19 09:09:37

这是一种间隙与岛屿问题。您正在尝试获取每组相同 CardNumber 值的排名数字(其中无间隙),按 ID DESC 排序时。

为此,您不能使用 DENSE_RANKROW_NUMBER,因为它们会将具有相同 CardNumber 值的所有行放在一起。

有多种解决方案。这是一个:

WITH PrevValues AS (
     SELECT *,
       IsNewCardNumber = CASE WHEN CardNumber = LAG(CardNumber) OVER (ORDER BY ID DESC)
                        THEN NULL ELSE 1 END
     FROM XXXX
)
SELECT
  ID,
  Rank = COUNT(IsNewCardNumber) OVER (ORDER BY ID DESC),
  CardNumber,
  StampNumber,
  AuditDate
FROM PrevValues;

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 by ID DESC.

You cannot use DENSE_RANK or ROW_NUMBER for this, because they will place all rows with the same CardNumber value together.

There are a number of solutions. Here is one:

WITH PrevValues AS (
     SELECT *,
       IsNewCardNumber = CASE WHEN CardNumber = LAG(CardNumber) OVER (ORDER BY ID DESC)
                        THEN NULL ELSE 1 END
     FROM XXXX
)
SELECT
  ID,
  Rank = COUNT(IsNewCardNumber) OVER (ORDER BY ID DESC),
  CardNumber,
  StampNumber,
  AuditDate
FROM PrevValues;

db<>fiddle

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文