sql query distinct with Row_Number

后端 未结 8 1137
死守一世寂寞
死守一世寂寞 2020-11-28 09:00

I am fighting with the distinct keyword in sql. I just want to display all row numbers of unique (distinct) values in a column & so I tried:

8条回答
  •  盖世英雄少女心
    2020-11-28 09:41

    How about something like

    ;WITH DistinctVals AS (
            SELECT  distinct id 
            FROM    table 
            where   fid = 64
        )
    SELECT  id,
            ROW_NUMBER() OVER (ORDER BY  id) AS RowNum
    FROM    DistinctVals
    

    SQL Fiddle DEMO

    You could also try

    SELECT distinct id, DENSE_RANK() OVER (ORDER BY  id) AS RowNum
    FROM @mytable
    where fid = 64
    

    SQL Fiddle DEMO

提交回复
热议问题