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

One thought on “select distinct row_number

Leave a comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s