是否可以使用 SQL 将身份添加到 GROUP BY?
是否可以向 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用的是 Sql Server 2005,您可以尝试使用 ROW_NUMBER
You could try using ROW_NUMBER if you are using Sql Server 2005
回报
returns