简体   繁体   中英

Grouping based on ROW_Number of each group

I had a requirement that grouping based on row_number of each group. Please view Image

图片-请查看此

SQL queries represent unordered sets. So, the distinction between the two groups for 47641 is undefined.

You can define a query that will assign a group that has exactly one fiberid for each scname . When there are multiples, the assignment is arbitrary.

To do so, you can use dense_rank() :

select t.*,
       (dense_rank() over (order by scname) - 1 + 
        row_number() over (partition by scname, fiberid order by fiberid)
       ) as grp
from t;

If you do have an ordering for the rows then a more stable assignment can be calculated.

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