database - Postgres: Query that can filter during table join -


i have postgres database duplicated entries on 1 of table. show created_by columns

table1

id | number 1  | 123 2  | 124 3  | 125 4  | 126 

table2

id | number | created_on 1  | 123    | 3/29 2  | 123    | 4/3 3  | 124    | 3/31 4  | 124    | 4/1 

on table 2 number duplicated. form single query list following:

id | number  | created_on 1  | 123     | 4/3 2  | 124     | 4/1 

for duplicated entries latest entry included. how form sql query?

select distinct on (table1.number) table1.id, table2.number, table2.create_on table1   join table2 on table1.number=table2.number order table2.create_on; 

actually tried putting 'distinct on' , 'order by' in single query (with join) gives me following error:

select distinct on expressions must match initial order expressions 

the columns in distinct on() have first ones in order by query, if want latest created_on date should order created_on desc

select distinct on (table1.number) table1.id, table2.number, table2.created_on  table1 join table2    on table1.number=table2.number order table1.number,table2.created_on desc; 

http://sqlfiddle.com/#!12/5538a/2


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -