Re: limit results to one row per foreign object

Поиск
Список
Период
Сортировка
От John Sidney-Woollett
Тема Re: limit results to one row per foreign object
Дата
Msg-id 44A5467E.8070900@wardbrook.com
обсуждение исходный текст
Ответ на limit results to one row per foreign object  ("Alan Bullock" <liststuff@gmail.com>)
Список pgsql-general
Without trying it out, how about something like:

select username, maxbid
from users u, (
   select user_id, max(amount) as maxbid
   from bids
   group by user_id
   where auction_id = XXX
) as b
where u.id = b.user_id;

John


Alan Bullock wrote:
> hi all, I have the following schema:
>
> CREATE TABLE auctions ( id serial NOT NULL, user_id int4, title
> varchar(255), body varchar(255), CONSTRAINT auctions_pkey PRIMARY KEY (id) )
> WITHOUT OIDS;
>
> CREATE TABLE bids ( id serial NOT NULL, auction_id int4, user_id int4,
> amount float8, created_at timestamp, CONSTRAINT bids_pkey PRIMARY KEY (id) )
> WITHOUT OIDS;
>
> CREATE TABLE users ( id serial NOT NULL, username varchar(255), CONSTRAINT
> users_pkey PRIMARY KEY (id) ) WITHOUT OIDS;
>
> I'd like to return all the bids for a given auction, but limit it to only
> the *latest* bid from each user. so regardless of how many bids a user has
> placed, only their latest is returned.
>
> I dont have a clue where to even start with this and would appreciate some
> pointers
> thanks
>
> alan
>
>
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 1: if posting/reading through Usenet, please send an appropriate
>        subscribe-nomail command to majordomo@postgresql.org so that your
>        message can get through to the mailing list cleanly

В списке pgsql-general по дате отправления:

Предыдущее
От: "paul rivers"
Дата:
Сообщение: Re: Notes on converting from MySQL 5.0.x to PostgreSQL
Следующее
От: Tom Lane
Дата:
Сообщение: Re: Notes on converting from MySQL 5.0.x to PostgreSQL