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
This entry was posted in Joy of T-SQL, T-SQL Tips & Tricks. Bookmark the permalink.

One Response to select distinct row_number

  1. Thank you very much Saeid that was just the wright information in just the moment i needed it! Have a nice day!

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s