Bringing other columns along with a GROUP BY clause

Поиск
Список
Период
Сортировка
От Rob Richardson
Тема Bringing other columns along with a GROUP BY clause
Дата
Msg-id 04A6DB42D2BA534FAC77B90562A6A03DC57467@server.rad-con.local
обсуждение исходный текст
Ответ на Re: Pet Peeves?  (Peter Eisentraut <peter_e@gmx.net>)
Ответы Re: Bringing other columns along with a GROUP BY clause  (Sam Mason <sam@samason.me.uk>)
Re: Bringing other columns along with a GROUP BY clause  (Tom Lane <tgl@sss.pgh.pa.us>)
Список pgsql-general
Greetings!

I've run into this problem a few times.  There must be a solution, but I
don't know what it is.

I have a table that has three interesting columns:  coil_id, charge, and
coldspot_time.  A charge can have several coils, so there are several
records with differing coil_ids but the same charge.  I want a list of
the coils whose coldspot_times are the largest for their charge.  I can
easily get a list of the maximum coldspot times for each charge:

Select charge, max(coldspot_time)
From inventory
Group by charge

But how do I find out what coil has the maximum coldspot time?

Select coil_id, charge, max(coldspot_time)
From inventory
Group by charge

Doesn't work because SQL can't read my mind well enough to know which of
the four coil_ids in the given charge is the one I want.

Thank you very much!

RobR

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

Предыдущее
От: Alvaro Herrera
Дата:
Сообщение: Re: Pet Peeves?
Следующее
От: Sam Mason
Дата:
Сообщение: Re: Bringing other columns along with a GROUP BY clause