X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id E069E9DCA1D for ; Mon, 13 Feb 2006 22:29:35 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 86923-03 for ; Mon, 13 Feb 2006 22:29:39 -0400 (AST) X-Greylist: domain auto-whitelisted by SQLgrey- Received: from web54305.mail.yahoo.com (web54305.mail.yahoo.com [206.190.49.115]) by postgresql.org (Postfix) with SMTP id 5D31D9DCA62 for ; Mon, 13 Feb 2006 22:29:33 -0400 (AST) Received: (qmail 77877 invoked by uid 60001); 14 Feb 2006 02:29:37 -0000 DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.com; h=Message-ID:Received:Date:From:Subject:To:In-Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding; b=kq6lNnsAlQGqYrLT5twYCLwOHcal26/paHxqznYln+Eewf5BEDyHnbPZ9ow6QBN201Mq+VSils+G4T6YiTXo+7G7Vb76ZPq4HjuXB7O/SE1qhP0QEE1wgDKSG1ecBOKzBoJ0egjeg6KSxG6MAW767y1vJRSo8R0ouoh1ayL+lYg= ; Message-ID: <20060214022937.77875.qmail@web54305.mail.yahoo.com> Received: from [68.54.4.83] by web54305.mail.yahoo.com via HTTP; Mon, 13 Feb 2006 18:29:37 PST Date: Mon, 13 Feb 2006 18:29:37 -0800 (PST) From: chester c young Subject: Re: group by complications To: Mark Fenbers , PostgreSQL - SQL In-Reply-To: <43F120F7.3000404@noaa.gov> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0.479 required=5 tests=[DNS_FROM_RFC_ABUSE=0.479] X-Spam-Score: 0.479 X-Spam-Level: X-Archive-Number: 200602/146 X-Sequence-Number: 24156 --- Mark Fenbers wrote: > select l.lid,l.fs,max(h.obstime) from location as l > inner join height as h on h.lid = l.lid > where l.fs > 0.0 > group by l.lid,l.fs; > > The above query works as expected in that is fetches the lid, fs and > time of the latest observation in the height table (for the > corresponding lid), but I also want to fetch (i.e., add to the select > list) the corresponding reading (h.obsvalue) which occurs at > max(h.obstime). I'm having trouble formulating the correct SQL > syntax > to pull out the l.lid, l.fs, and the most recent h.obvalue (with or > without the time that it occurred). > > Logistically, I want to do something like this: > > select l.lid,l.fs,most_recent(h.obsvalue) from location as l > inner join height as h on h.lid = l.lid > where l.fs > 0.0 > group by l.lid,l.fs; > use your original query as part of the from clause, then add columns to it through a subquery or a join. try something like this: select q1.*, (select obsvalue from height where lid=q1.lid and obstime=q1.obstime) as obsvalue from (select l.lid,l.fs,max(h.obstime) as obstime1 from location as l inner join height as h on h.lid = l.lid where l.fs > 0.0 group by l.lid,l.fs ) q1; __________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com