是否可以使用 SQL 将身份添加到 GROUP BY?

发布于 2024-08-18 09:24:57 字数 626 浏览 10 评论 0原文

是否可以向 GROUP BY 添加标识列,以便每个重复项都有一个标识号?

我的原始数据如下所示:

1    AAA  [timestamp]
2    AAA  [timestamp]
3    BBB  [timestamp]
4    CCC  [timestamp]
5    CCC  [timestamp]
6    CCC  [timestamp]
7    DDD  [timestamp]
8    DDD  [timestamp]
9    EEE  [timestamp]
....

我想将其转换为:

1    AAA   1
2    AAA   2
4    CCC   1
5    CCC   2
6    CCC   3
7    DDD   1
8    DDD   2
...

解决方案是:

CREATE PROCEDURE [dbo].[RankIt]
AS
BEGIN
SET NOCOUNT ON;

SELECT  *, RANK() OVER(PARTITION BY col2 ORDER BY timestamp DESC) AS ranking 
FROM MYTABLE;

END

Is it possible to add a identity column to a GROUP BY so that each duplicate has a identity number?

My original data looks like this:

1    AAA  [timestamp]
2    AAA  [timestamp]
3    BBB  [timestamp]
4    CCC  [timestamp]
5    CCC  [timestamp]
6    CCC  [timestamp]
7    DDD  [timestamp]
8    DDD  [timestamp]
9    EEE  [timestamp]
....

And I want to convert it to:

1    AAA   1
2    AAA   2
4    CCC   1
5    CCC   2
6    CCC   3
7    DDD   1
8    DDD   2
...

The solution was:

CREATE PROCEDURE [dbo].[RankIt]
AS
BEGIN
SET NOCOUNT ON;

SELECT  *, RANK() OVER(PARTITION BY col2 ORDER BY timestamp DESC) AS ranking 
FROM MYTABLE;

END

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

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

发布评论

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

评论(2

扶醉桌前 2024-08-25 09:24:57

如果您使用的是 Sql Server 2005,您可以尝试使用 ROW_NUMBER

DECLARE @Table TABLE(
        ID INT,
        Val VARCHAR(10)
)

INSERT INTO @Table SELECT 1,'AAA'
INSERT INTO @Table SELECT 2,'AAA'
INSERT INTO @Table SELECT 3,'BBB' 
INSERT INTO @Table SELECT 4,'CCC' 
INSERT INTO @Table SELECT 5,'CCC' 
INSERT INTO @Table SELECT 6,'CCC' 
INSERT INTO @Table SELECT 7,'DDD' 
INSERT INTO @Table SELECT 8,'DDD' 
INSERT INTO @Table SELECT 9,'EEE' 

SELECT  *,
        ROW_NUMBER() OVER(PARTITION BY VAL ORDER BY Val)
FROM    @Table

You could try using ROW_NUMBER if you are using Sql Server 2005

DECLARE @Table TABLE(
        ID INT,
        Val VARCHAR(10)
)

INSERT INTO @Table SELECT 1,'AAA'
INSERT INTO @Table SELECT 2,'AAA'
INSERT INTO @Table SELECT 3,'BBB' 
INSERT INTO @Table SELECT 4,'CCC' 
INSERT INTO @Table SELECT 5,'CCC' 
INSERT INTO @Table SELECT 6,'CCC' 
INSERT INTO @Table SELECT 7,'DDD' 
INSERT INTO @Table SELECT 8,'DDD' 
INSERT INTO @Table SELECT 9,'EEE' 

SELECT  *,
        ROW_NUMBER() OVER(PARTITION BY VAL ORDER BY Val)
FROM    @Table
梦与时光遇 2024-08-25 09:24:57
create table #testalot
(
  [id] int identity,
  data varchar(50)
)

insert #testalot (data) values('AAA')
insert #testalot (data) values('AAA')
insert #testalot (data) values('BBB')
insert #testalot (data) values('CCC')
insert #testalot (data) values('CCC')
insert #testalot (data) values('CCC')
insert #testalot (data) values('DDD')
insert #testalot (data) values('DDD')

select *,ROW_NUMBER() OVER(PARTITION BY data ORDER BY data DESC) AS 'Number'
 from #testalot

 drop table #testalot

回报

id  data Number
1   AAA  1
2   AAA  2
3   BBB  1
4   CCC  1
5   CCC  2
6   CCC  3
7   DDD  1
8   DDD  2
create table #testalot
(
  [id] int identity,
  data varchar(50)
)

insert #testalot (data) values('AAA')
insert #testalot (data) values('AAA')
insert #testalot (data) values('BBB')
insert #testalot (data) values('CCC')
insert #testalot (data) values('CCC')
insert #testalot (data) values('CCC')
insert #testalot (data) values('DDD')
insert #testalot (data) values('DDD')

select *,ROW_NUMBER() OVER(PARTITION BY data ORDER BY data DESC) AS 'Number'
 from #testalot

 drop table #testalot

returns

id  data Number
1   AAA  1
2   AAA  2
3   BBB  1
4   CCC  1
5   CCC  2
6   CCC  3
7   DDD  1
8   DDD  2
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文