DENSE_RANK according to particular order

前端 未结 2 442
天涯浪人
天涯浪人 2021-01-18 05:59

Hi I have a table of data I want to output the dense_rank of the names starting from the first group of names according to sorted dates order. e.g.

DROP TABL         


        
2条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-18 06:43

    First rank distinct names ordered by date and then join on the table:

    ;WITH cte AS(SELECT name, ROW_NUMBER() OVER(ORDER BY MIN(date)) rn 
                 FROM dbo.MyTable 
                 GROUP BY name)
    SELECT c.rn, m.date, m.name
    FROM cte c
    JOIN dbo.MyTable m ON m.name = c.name
    ORDER BY m.date
    

提交回复
热议问题