Re: How can I get the last element out of GROUP BY sets? - Mailing list pgsql-sql

From Robert Creager
Subject Re: How can I get the last element out of GROUP BY sets?
Date
Msg-id 20040119103853.01c87b83.Robert_Creager@LogicalChaos.org
Whole thread Raw
In response to Re: How can I get the last element out of GROUP BY sets?  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-sql
When grilled further on (Mon, 19 Jan 2004 00:44:30 -0500),
Tom Lane <tgl@sss.pgh.pa.us> confessed:

> Robert Creager <Robert_Creager@LogicalChaos.org> writes:
> > ... one piece of data I need is the last value for each GROUP BY
> > period.  Alas, I cannot figure out how to do this.
> 
> SELECT DISTINCT ON (rather than GROUP BY) could get this done for you.
> 

I had my whine all ready as to how I still couldn't figure it out, when I
figured it out:

...
SELECT p.period, etday
FROM (SELECT DISTINCT ON ( period )        date_trunc( 'hour', "when" ) AS period,        etday     FROM readings
ORDERBY period, "when" DESC) AS p
 
...

Thanks for the tip.

Cheers,
Rob

-- 08:10:55 up 21 days, 21:58,  4 users,  load average: 2.15, 2.06, 2.02

pgsql-sql by date:

Previous
From: Tom Lane
Date:
Subject: Re: name of a column returned from a table function
Next
From: "V i s h a l Kashyap @ [Sai Hertz And Control Systems]"
Date:
Subject: Re: Trigger to identify which column(s) updated