Change select distinct SQL statement to work on SQL Server 2008 -


i have sql statement works on postgresql , returns 2 columns ordering other columns:

select distinct on (title) gender_id, title  persons account_id = 100 order title, created_date 

i need change sql work on sql server 2008 , postgres, in sql server can't use on clause how can it?

use row_number():

select p.* (select p.*,              row_number() on (partition title order created_date) seqnum       persons p       account_id = 100      ) p seqnum = 1; 

this version work in both postgres , sql server.

note: in postgres, distinct on faster using row_number().


Comments

Popular posts from this blog

c - How to retrieve a variable from the Apache configuration inside the module? -

c# - Constructor arguments cannot be passed for interface mocks -

python - malformed header from script index.py Bad header -