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:
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
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