如何使用没有临时表的SQL查询为组中的每个元素添加序列号 [英] How to add sequence number for each element in a group using a SQL query without temp tables

查看:150
本文介绍了如何使用没有临时表的SQL查询为组中的每个元素添加序列号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

但是,我需要枚举组的出现。最终输出如下:

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

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

SQL。任何想法?

推荐答案

可能是这样的:

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 GroupSequence2 GroupSequence3 将获得您想要的输出。

GroupSequence1, GroupSequence2 and GroupSequence3 will get you the output you want.

这篇关于如何使用没有临时表的SQL查询为组中的每个元素添加序列号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆