[SQL] the best way to get the first record from each group

2005-02-08 Thread q2005
Hi, Is there any better alternative to get the first record from each group? "subno" is an integer, record with the smallest subno in each group is the first record in the group. select itemno, measureunit, extaxprice from itmt_purchase where subno in (select min(subno)as subno from itmt_pu

Re: [SQL] the best way to get the first record from each group

2005-02-07 Thread Michael Fuhr
On Tue, Feb 08, 2005 at 03:20:21AM +0100, PFC wrote: > Anyway, it was fun to experiment with that ! Interesting -- thanks for taking the time. -- Michael Fuhr http://www.fuhr.org/~mfuhr/ ---(end of broadcast)--- TIP 9: the planner will ignore you

Re: [SQL] the best way to get the first record from each group

2005-02-07 Thread PFC
I don't really gr0k your field names so I'll use an easier example : CREATE TABLE groups ( group_id SERIAL PRIMARY KEY, group_name TEXT NULL ) WITHOUT OIDS; CREATE TABLE people ( user_id SERIAL PRIMARY KEY, group_id INTEGER NOT NULL REFERENCES groups(group_id), score INTEGER NOT NULL ) WITH

Re: [SQL] the best way to get the first record from each group

2005-02-07 Thread Michael Fuhr
On Tue, Feb 08, 2005 at 10:36:17AM +1000, q2005 wrote: > > Is there any better alternative to get the first record from each group? PostgreSQL has a non-standard SELECT DISTINCT ON query for just this purpose. http://www.postgresql.org/docs/8.0/static/queries-select-lists.html#QUERIES-DISTINCT ht

[SQL] the best way to get the first record from each group

2005-02-07 Thread q2005
Hi, Is there any better alternative to get the first record from each group? "subno" is an integer. The record with the smallest subno in each group is the first record in the group. select itemno, measureunit, extaxprice from itmt_purchase where subno in (select min(subno)as subno from itm