条件复杂的 Row_Number

发布于 2024-12-14 11:21:04 字数 941 浏览 0 评论 0原文

除了使用 Group By 完成的简单分区之外,是否可以使用 Row_Number() 对其他内容上的行进行编号?

这是我的特殊情况:

Id    Type    Date
--    ----    ----
 1      16    Some Date
 2      16    Some Date   
 3      16    Some Date
 4      32    Some Date
 5      64    Some Date
 6      64    Some Date
 7     128    Some Date
 8     256    Some Date
 9     256    Some Date
10     256    Some Date

我想按以下方式对编号进行分区(行编号按日期排序):

Id    Type    RowNb
--    ----    -----
 6      64        1
 4      32        2
 5      64        3
 9     256        1
 3      16        2
 1      16        3
 8     256        4
 7     128        5
 2      16        6
10     256        7

即:除 32 和 64 之外的所有其他类型都一起编号。类型 32 和 64 的编号是可选的,因为我只需要对其他类型进行排序。

我真正想做的是检索类型为 32 和 64 的所有行以及其他类型中日期最低的行。我问这个具体问题的原因是,将来我可能需要检索的不仅仅是第一列,而且我认为如果我可以像这样对行进行编号,会更容易。如果您有其他解决方案,我洗耳恭听。

Is it possible to use Row_Number() to number rows on something else than a simple partition done with Group By ?

This is my particular case :

Id    Type    Date
--    ----    ----
 1      16    Some Date
 2      16    Some Date   
 3      16    Some Date
 4      32    Some Date
 5      64    Some Date
 6      64    Some Date
 7     128    Some Date
 8     256    Some Date
 9     256    Some Date
10     256    Some Date

I want to partition the numbering in the following way (row numbering is sorted by date) :

Id    Type    RowNb
--    ----    -----
 6      64        1
 4      32        2
 5      64        3
 9     256        1
 3      16        2
 1      16        3
 8     256        4
 7     128        5
 2      16        6
10     256        7

ie: Every other type than 32 and 64 are numbered together. The numbering of types 32 and 64 is optional because I only need to sort the others one.

What I really want to do, is retrieve all the rows with type 32 and 64 and only the row with the lowest date from the other type. The reason why I'm asking this specific question is that in the future it is possible that I will have to retrieve more than just the first column and I think it will be easier if I can number my rows like that. If you have another solution, I'm all ears.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

り繁华旳梦境 2024-12-21 11:21:04

当然,您可以拥有复杂的分区表达式,就像您可以拥有复杂的分组表达式一样。以下是对示例中的行进行排名的一种可能方法:

SELECT
  Id,
  Type,
  ROW_NUMBER() OVER (
    PARTITION BY CASE WHEN Type IN (32, 64) THEN 1 ELSE 2 END
    Order BY Date
  )
FROM atable

但是稍后在过滤要显示的行时必须重复该条件。所以,如果我是你,我可能会尝试这样的事情:

WITH marked AS (
  SELECT
    *,
    TypeGroup = CASE WHEN Type IN (32, 64) THEN 1 ELSE 2 END
  FROM atable
),
ranked AS (
  SELECT
    *,
    RowNb = ROW_NUMBER() OVER (PARTITION BY TypeGroup ORDER BY Date)
  FROM
)
SELECT
  Id,
  Type,
  RowNb
FROM ranked
WHERE TypeGroup = 1 OR RowNb = 1
ORDER BY TypeGroup, RowNb

Of course you can have complex partitioning expressions, just like you could have complex grouping expressions. Here's one possible way how you could rank rows in your example:

SELECT
  Id,
  Type,
  ROW_NUMBER() OVER (
    PARTITION BY CASE WHEN Type IN (32, 64) THEN 1 ELSE 2 END
    Order BY Date
  )
FROM atable

But you'd have to repeat that condition later when filtering rows to display. So, if I were you, I might try something like this:

WITH marked AS (
  SELECT
    *,
    TypeGroup = CASE WHEN Type IN (32, 64) THEN 1 ELSE 2 END
  FROM atable
),
ranked AS (
  SELECT
    *,
    RowNb = ROW_NUMBER() OVER (PARTITION BY TypeGroup ORDER BY Date)
  FROM
)
SELECT
  Id,
  Type,
  RowNb
FROM ranked
WHERE TypeGroup = 1 OR RowNb = 1
ORDER BY TypeGroup, RowNb
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文