[英]How to get same rownumber() for same values
Need to get the same "Row Number" if the values gets repeated in Column Week and Desc. 如果列周和描述中的值重复,则需要获得相同的“行号”。 For the following table:
对于下表:
╔════════╦═══════╦
║ Week ║ Desc ║
╠════════╬═══════╬
║ 1 ║ FF ║
║ 1 ║ ss ║
║ 1 ║ ss ║
║ 2 ║ FF ║
║ 2 ║ ss ║
║ 4 ║ FF ║
║ 4 ║ FF ║
║ 4 ║ ss ║
║ 4 ║ ss ║
╚════════╩═══════╝
The expected result is: 预期结果是:
╔════════╦═══════╦════════╗
║ Week ║ Desc ║ RowNum ║
╠════════╬═══════╬════════╬
║ 1 ║ FF ║ 1 ║
║ 1 ║ ss ║ 2 ║
║ 1 ║ ss ║ 2 ║
║ 2 ║ FF ║ 1 ║
║ 2 ║ ss ║ 2 ║
║ 4 ║ FF ║ 1 ║
║ 4 ║ FF ║ 1 ║
║ 4 ║ ss ║ 2 ║
║ 4 ║ ss ║ 2 ║
╚════════╩═══════╩════════╝
You want DENSE_RANK
instead of ROW_NUMBER
: 您需要
DENSE_RANK
而不是ROW_NUMBER
:
SELECT Week
, [Desc]
, DENSE_RANK() OVER (PARTITION BY Week ORDER BY [Desc]) AS [Rank #]
FROM t
DENSE_RANK
and RANK
assign same value to rows with tie in the order by columns. DENSE_RANK
和RANK
将相同的值分配给按列顺序打领带的行。 DENSE_RANK
in addition assigns "dense" rank numbers instead of "gapped" numbers. 另外,
DENSE_RANK
分配“密集”等级编号,而不是“间隙”编号。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.