Skip to content
Advertisement

sql query distinct with Row_Number

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:

however the below code giving me the distinct values:

but when tried it with Row_Number.
then it is not working.

Advertisement

Answer

Use this:

and put the “output” of a query as the “input” of another.

Using CTE:

The two queries should be equivalent.

Technically you could

but if you increase the number of DISTINCT fields, you have to put all these fields in the PARTITION BY, so for example

I even hope you comprehend that you are going against standard naming conventions here, id should probably be a primary key, so unique by definition, so a DISTINCT would be useless on it, unless you coupled the query with some JOINs/UNION ALL

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement