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++ - llvm function pass ReplaceInstWithInst malloc -

Cross-Compiling Linux Kernel for Raspberry Pi - ${CCPREFIX}gcc -v does not work -

java.lang.NoClassDefFoundError When Creating New Android Project -