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 A50E59DCA62 for ; Mon, 13 Feb 2006 20:14:54 -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 63636-02 for ; Mon, 13 Feb 2006 20:14:56 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey- Received: from ermsgsvr.werh.noaa.gov (ermsgsvr.werh.noaa.gov [198.206.32.50]) by postgresql.org (Postfix) with ESMTP id EDB879DC821 for ; Mon, 13 Feb 2006 20:14:51 -0400 (AST) Received: from [198.206.42.50] (hopper.wiln.noaa.gov [198.206.42.50]) by ermsgsvr.werh.noaa.gov (iPlanet Messaging Server 5.2 HotFix 2.01 (built Aug 26 2004)) with ESMTPSA id <0IUN00FOWI0SGX@ermsgsvr.werh.noaa.gov> for pgsql-sql@postgresql.org; Mon, 13 Feb 2006 19:14:54 -0500 (EST) Date: Mon, 13 Feb 2006 19:14:47 -0500 From: Mark Fenbers Subject: group by complications To: PostgreSQL - SQL Message-id: <43F120F7.3000404@noaa.gov> Organization: DOC/NOAA/NWS/OHRFC MIME-version: 1.0 Content-type: text/plain; charset=ISO-8859-1; format=flowed Content-transfer-encoding: 7BIT X-Accept-Language: en-us, en User-Agent: Mozilla Thunderbird 1.0.7 (Windows/20050923) X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0.12 required=5 tests=[AWL=0.120] X-Spam-Score: 0.12 X-Spam-Level: X-Archive-Number: 200602/143 X-Sequence-Number: 24153 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; Can someone offer hints, please? Mark