Grouping by or iterating through partitions in SQL

前端 未结 1 415
别跟我提以往
别跟我提以往 2021-01-25 16:02

Two part question regarding partitioning in SQL.

In T-SQL when you use PARTITION BY is there a way to assign a unique number to each partition, in addition to something

相关标签:
1条回答
  • 2021-01-25 16:26

    You could use dense_rank():

    select  *
    ,       row_number() over (partition by Action order by Timestamp) as RowNum
    ,       dense_rank() over (order by Action) as PartitionNum
    from    YourTable
    

    Example at SQL Fiddle.

    T-SQL is not good at iterating, but if you really have to, check out cursors.

    0 讨论(0)
提交回复
热议问题