select distinct row_number

 

SELECT DISTINCT
       col ,
       ROW_NUMBER() OVER ( ORDER BY col) AS rownum
     FROM test_table

As you see in above query, when we want use DISTINCT with ROW_NUMBER, we get strange result.This is because the ROW_NUMBER is processed before the DISTINCT clause.So we can use this one:

SELECT
       col ,
       ROW_NUMBER() OVER ( ORDER BY col) AS rownum
     FROM test_table
        GROUP BY col
Advertisements