Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WFWSU-00068B-R9 for pgsql-sql@arkaria.postgresql.org; Mon, 17 Feb 2014 22:14:39 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1WFWSU-0000Gh-7w for pgsql-sql@arkaria.postgresql.org; Mon, 17 Feb 2014 22:14:38 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WFWQl-0000GE-L8 for pgsql-sql@postgresql.org; Mon, 17 Feb 2014 22:12:51 +0000 Received: from [216.139.236.26] (helo=sam.nabble.com) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WFWQe-0006mD-1j for pgsql-sql@postgresql.org; Mon, 17 Feb 2014 22:12:51 +0000 Received: from [192.168.236.26] (helo=sam.nabble.com) by sam.nabble.com with esmtp (Exim 4.72) (envelope-from ) id 1WFWQb-0005k0-Be for pgsql-sql@postgresql.org; Mon, 17 Feb 2014 14:12:41 -0800 Date: Mon, 17 Feb 2014 14:12:41 -0800 (PST) From: David Johnston To: pgsql-sql@postgresql.org Message-ID: <1392675161284-5792491.post@n5.nabble.com> In-Reply-To: References: Subject: Re: include ids in query grouped by multipe values MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Host-Lookup-Failed: Reverse DNS lookup failed for 216.139.236.26 (deferred) X-Pg-Spam-Score: 2.5 (++) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-sql Precedence: bulk Sender: pgsql-sql-owner@postgresql.org Karsten-3-2 wrote > 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; > [Not Tested] SELECT b.IDone, b.IDtwo, b.sale, array_agg(a) AS matching_on_tableA FROM TableA a NATURAL JOIN ( SELECT IDone, IDtwo, max(sale) AS sale FROM TableA GROUP BY 1, 2 ) b GROUP BY 1, 2, 3; In this solution you simply save the entire tableA record, as a composite typed column, into an array so that you now have a single row for each "IDone, IDtwo, (max)sale" combination - which you omitted in the description above - but can still access to relevant matching rows using the array. Add "ORDER BY" - e.g., array_agg(...ORDER BY) - to setup a desired sort order. You can do something like (against, not tested): SELECT IDone, IDtwo, sale, (array_agg[0]).* AS row_from_tablea FROM to get to the relevant data in the array. David J. -- View this message in context: http://postgresql.1045698.n5.nabble.com/include-ids-in-query-grouped-by-multipe-values-tp5792470p5792491.html Sent from the PostgreSQL - sql mailing list archive at Nabble.com. -- Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql