sql query distinct with Row_Number

后端 未结 8 976
死守一世寂寞
死守一世寂寞 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:51

    This can be done very simple, you were pretty close already

    SELECT distinct id, DENSE_RANK() OVER (ORDER BY  id) AS RowNum
    FROM table
    WHERE fid = 64
    
    0 讨论(0)
  • 2020-11-28 09:55

    Using DISTINCT causes issues as you add fields and it can also mask problems in your select. Use GROUP BY as an alternative like this:

    SELECT id
          ,ROW_NUMBER() OVER (ORDER BY  id) AS RowNum
      FROM table
     where fid = 64
     group by id
    

    Then you can add other interesting information from your select like this:

    ,count(*) as thecount
    

    or

    ,max(description) as description
    
    0 讨论(0)
提交回复
热议问题