简体   繁体   English

SQL 行计数超过分区

[英]SQL Row Count Over Partition By

As we know, over partition by increases until the group changes.众所周知,过度分区会增加,直到组发生变化。 When the group is changed, it starts over.当组更改时,它会重新开始。 How can the opposite be done?怎么能反其道而行之? that is, if the group is not changed, the number should repeat as follows.也就是说,如果组未更改,则编号应重复如下。

NAME | ROW_COUNT
 A        1
 A        1
 A        1
 B        2
 C        3
 C        3
 D        4
 E        5

Your scenario is of using dense_rank() as rank() doesnt mantain the sequence but just ranks the column also row_number() maintains the sequence but again in case of similar rank it assigns it a unique number您的场景是使用dense_rank()因为rank()不维护序列,但只是对列进行排名, row_number()维护序列,但再次在相似等级的情况下,它会为其分配一个唯一编号

    Select name, dense_rank() over (partition by name order by 
   name) from table;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM