条件复杂的 Row_Number
除了使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当然,您可以拥有复杂的分区表达式,就像您可以拥有复杂的分组表达式一样。以下是对示例中的行进行排名的一种可能方法:
但是稍后在过滤要显示的行时必须重复该条件。所以,如果我是你,我可能会尝试这样的事情:
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:
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: