增加组上的行号 [英] Increment Row Number on Group
本文介绍了增加组上的行号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在处理 SQL Server 2005 的查询,该查询需要返回带有两个索引"字段的数据.每次 'shade' 列更改时,第一个索引 't_index' 应增加,而第二个索引在 'shade' 列中的值的分区内增加:
I am working on a query for SQL Server 2005 that needs to return data with two 'index' fields. The first index 't_index' should increment every time the 'shade' column changes, whilst the second index increments within the partition of the values in the 'shade' column:
t_index s_index shade
1 1 A
1 2 A
1 3 A
1 4 A
1 5 A
2 1 B
2 2 B
2 3 B
2 4 B
2 5 B
要获取 s_index 列,我使用了以下内容:
To get the s_index column I am using the following:
Select ROW_NUMBER() OVER(PARTITION BY [shade] ORDER BY [shade]) as s_index
我的问题是如何让第一个索引仅在 'shade' 列中的值发生变化时增加?
My question is how to get the first index to only increment when the value in the 'shade' column changes?
推荐答案
这可以通过 DENSE_RANK()
函数来完成:
That can be accomplished with the DENSE_RANK()
function:
DENSE_RANK() OVER(Order By [shade]) as t_index
这篇关于增加组上的行号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文