sorry, first answer not to the list ... ---------- Ursprüngliche Nachricht ---------- Von: Andreas Kretschmer <andreas@xxxxxxxxxxxxxxx> An: Antonio Parrotta <antonioparrotta@xxxxxxxxx> Datum: 28. Dezember 2012 um 13:00 Betreff: Re: [SQL] sql basic question > > so the result should be: > LABEL ID Distance SIDE > "15"; 119006; 0.10975569030617; 1 > "19"; 64056; 0.41205442839764; 1 > "14"; 64054; 0.118448307450912; 0 > "24"; 119007; 0.59758734628752; 0 > > test=*# select * from foo; label | id | distance | side -------+--------+-------------------+------ 15 | 119006 | 0.10975569030617 | 1 14 | 64054 | 0.118448307450912 | 0 16 | 64055 | 0.176240407317772 | 0 20 | 64057 | 0.39363711745035 | 0 19 | 64056 | 0.41205442839764 | 1 24 | 119007 | 0.59758734628752 | 0 (6 rows) test=*# select * from (select distinct on (side) label, id, distance, side from foo order by side, distance) a union all (select distinct on (side) label, id, distance, side from foo order by side, distance desc) order by side desc, label; label | id | distance | side -------+--------+-------------------+------ 15 | 119006 | 0.10975569030617 | 1 19 | 64056 | 0.41205442839764 | 1 14 | 64054 | 0.118448307450912 | 0 24 | 119007 | 0.59758734628752 | 0 (4 rows) HTH, Andreas -- Sent via pgsql-general mailing list (pgsql-general@xxxxxxxxxxxxxx) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-general