oracle 如何使用没有临时表的 SQL 查询为组中的每个元素添加序列号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10244594/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 00:44:24  来源:igfitidea点击:

How to add sequence number for each element in a group using a SQL query without temp tables

sqldatabaseoraclegrouping

提问by user1346265

My question is quite similar to the one posted in this link - How to add sequence number for groups in a SQL query without temp tables

我的问题与此链接中发布的问题非常相似 -如何在没有临时表的 SQL 查询中为组添加序列号

But, I need to enumerate the occurrence of group. The final output to be like this:

但是,我需要枚举 group 的发生。最终输出是这样的:

Record  Group     GroupSequence
-------|---------|--------------
1       Chickens  1
2       Chickens  2
3       Cows      1
4       Horses    1
5       Horses    2
6       Horses    3

Plus this has to be done in Oracle SQL. Any ideas?

此外,这必须在 Oracle SQL 中完成。有任何想法吗?

回答by Arion

Maybe something like this:

也许是这样的:

SELECT
    ROW_NUMBER() OVER(PARTITION BY [Group] ORDER BY Record) AS GroupSequence1,
    RANK() OVER(PARTITION BY [Group] ORDER BY Record) AS GroupSequence2,
    DENSE_RANK() OVER(PARTITION BY [Group] ORDER BY Record) AS GroupSequence3,
    Table1.Group,
    Table1.Record
FROM
    Table1

GroupSequence1, GroupSequence2and GroupSequence3will get you the output you want.

GroupSequence1GroupSequence2GroupSequence3会得到你想要的输出。