Re: [GENERAL] DISTINCT ON without ORDER BY

2009-04-21 Thread Martijn van Oosterhout
On Tue, Apr 21, 2009 at 12:11:26PM +, Jasen Betts wrote: > > Is there a way to acheive the above result without a sort and without a > > self-join? > > anyway you could possibly write an agregate function that returns a > copy of the row with the highest id? Put that way it sounds like someth

Re: [GENERAL] DISTINCT ON without ORDER BY

2009-04-21 Thread Jasen Betts
On 2009-04-19, Martijn van Oosterhout wrote: > Hi, > > I was going through the queries of an SQL application and came across > queries like: > > SELECT * FROM foo > WHERE id in (SELECT max(id) FROM foo GROUP BY bar); > > I thought, here's a case where this could be better written using > DISTINCT

Re: [GENERAL] DISTINCT ON without ORDER BY

2009-04-20 Thread Gregory Stark
Martijn van Oosterhout writes: > SELECT * FROM foo > WHERE id in (SELECT max(id) FROM foo GROUP BY bar); > > Is there a way to acheive the above result without a sort and without a > self-join? Something like SELECT bar, (magic_agg_func(foo)).* FROM foo GROUP BY bar where you define an aggrega

[GENERAL] DISTINCT ON without ORDER BY

2009-04-19 Thread Martijn van Oosterhout
Hi, I was going through the queries of an SQL application and came across queries like: SELECT * FROM foo WHERE id in (SELECT max(id) FROM foo GROUP BY bar); I thought, here's a case where this could be better written using DISTINCT ON, since then you avoid the self-join: SELECT DISTINCT ON (ba