简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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