X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 91DF95E46CD for ; Tue, 17 Aug 2004 09:22:57 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 42201-08 for ; Tue, 17 Aug 2004 12:22:50 +0000 (GMT) Received: from matrix.gatewaynet.com (matrix.gatewaynet.com [217.19.69.50]) by svr1.postgresql.org (Postfix) with ESMTP id 799495E37D0 for ; Tue, 17 Aug 2004 09:22:45 -0300 (ADT) Received: from matrix.gatewaynet.com (localhost.localdomain [127.0.0.1]) by matrix.gatewaynet.com (8.12.8/8.12.8) with ESMTP id i7HBZ44T031530; Tue, 17 Aug 2004 14:35:04 +0300 Received: from localhost (achill@localhost) by matrix.gatewaynet.com (8.12.8/8.12.8/Submit) with ESMTP id i7HBZ4O9031526; Tue, 17 Aug 2004 14:35:04 +0300 Date: Tue, 17 Aug 2004 14:35:04 +0300 (EEST) From: Achilleus Mantzios To: Dino Vliet Cc: pgsql-sql@postgresql.org Subject: Re: only last records in subgroups In-Reply-To: <20040817104437.93301.qmail@web40110.mail.yahoo.com> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.0 tagged_above=0.0 required=5.0 tests= X-Spam-Level: X-Archive-Number: 200408/161 X-Sequence-Number: 18658 O kyrios Dino Vliet egrapse stis Aug 17, 2004 : > Hi there, > > I'm having hard times with the following query: > I want to select only the last records from a subgroup > in a table. But because the subgroup contains > different number of records for every id, I don't know > how to specify that. For example, check the following > table, > id, day > x,300 > x,250, > x,0 > y,250 > y,4 > > I only want the records > x,0 and y,4 but how do I manage this in sql? I was > hoping for a keyword LAST or so, where I can specify > that when I've ordered my results with order by, I > could only get the last of the subgroups (the first > one is easy because I could use limit 1) > Try, SELECT min(day),id from reg group by id; min | id -----+---- 4 | y 0 | x (2 rows) > Thanks in advance > > > > > __________________________________ > Do you Yahoo!? > New and Improved Yahoo! Mail - 100MB free storage! > http://promotions.yahoo.com/new_mail > > ---------------------------(end of broadcast)--------------------------- > TIP 5: Have you checked our extensive FAQ? > > http://www.postgresql.org/docs/faqs/FAQ.html > -- -Achilleus