include ids in query grouped by multipe values

Поиск
Список
Период
Сортировка
От karsten
Тема include ids in query grouped by multipe values
Дата
Msg-id C2D599BB8E04404B8FB0169FA17B5248@terragis2
обсуждение исходный текст
Ответы Re: include ids in query grouped by multipe values  (Carlos Chapi <carlos.chapi@2ndquadrant.com>)
Re: include ids in query grouped by multipe values  (David Johnston <polobo@yahoo.com>)
Список pgsql-sql
Hi Group,
 
for several days I have been trying to resolve the following task below, but all my attempts so far appear to give me back too many rows:
 
I have data kind like this in TableA:
 
id   IDone  IDtwo  sale
1    010    200    8000
2    010    200    7851
3    010    200    517
4    020    210    5730
5    020    210    2000
6    020    230    3170
7    020    230    3170
8    020    230    2051
9    030    230    0
 
With the query below I can basically select the maximum sale for each IDone - IDtwo combination - so almost what I need:
 
select IDone, IDtwo, max(sale) as maxsale FROM TableA group by IDone, IDtwo;
 
But it would want to also select the id column ( and all other additonal 20 columns of TabelA not shown above) and need that there is only one record returned for each IDone - IDtwo combination. I tried
 
SELECT a.id, a.IDone, a.IDtwo, a.sale FROM TableA a
inner join ( select IDone, IDtwo, max(sale) as maxsale FROM TableA group by IDone, IDtwo ) b
on a.IDone = b.IDone and b.IDtwo = b.IDtwo  
and a.sale = b.maxsale;
 
but that returns too many rows and I do not understand why
How can I resolve this ?
 
Karsten Vennemann
Terra GIS LTD

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

Предыдущее
От: Adrian Klaver
Дата:
Сообщение: Re: Trigger SQL script execution after DROP EXTENSION
Следующее
От: Carlos Chapi
Дата:
Сообщение: Re: include ids in query grouped by multipe values