pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Mark Fenbers <Mark.Fenbers@noaa.gov>
To: PostgreSQL - SQL <pgsql-sql@postgresql.org>
Subject: group by complications
Date: Mon, 13 Feb 2006 19:14:47 -0500
Message-ID: <43F120F7.3000404@noaa.gov> (raw)

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



view thread (4+ messages)  latest in thread

Message-ID: <43F120F7.3000404@noaa.gov>
Permalink:  ../43F120F7.3000404@noaa.gov/
Also on:    postgresql.org/message-id/43F120F7.3000404@noaa.gov

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: Mark.Fenbers@noaa.gov
  Subject: Re: group by complications
  In-Reply-To: <43F120F7.3000404@noaa.gov>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox