如何使用SQL查询,而无需临时表添加序列号为每个元素的组(How to add sequence n

2019-06-23 19:44发布

我的问题是非常相似的一个张贴在这个环节- 如何在SQL查询中添加序列号组,而不用临时表

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

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

加上这在甲骨文的SQL工作要做。 有任何想法吗?

Answer 1:

也许是这样的:

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

GroupSequence1GroupSequence2GroupSequence3会得到你想要的输出。



文章来源: How to add sequence number for each element in a group using a SQL query without temp tables