include ids in query grouped by multipe values - Mailing list pgsql-sql

From karsten
Subject include ids in query grouped by multipe values
Date
Msg-id C2D599BB8E04404B8FB0169FA17B5248@terragis2
Whole thread Raw
Responses Re: include ids in query grouped by multipe values
Re: include ids in query grouped by multipe values
List 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 by date:

Previous
From: Adrian Klaver
Date:
Subject: Re: Trigger SQL script execution after DROP EXTENSION
Next
From: Carlos Chapi
Date:
Subject: Re: include ids in query grouped by multipe values