Skip site navigation (1) Skip section navigation (2)

Re: How can I get the last element out of GROUP BY sets?

From: Robert Creager <Robert_Creager(at)LogicalChaos(dot)org>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, PGSQL <pgsql-sql(at)postgresql(dot)org>,PGSQL <pgsql-sql(at)postgresql(dot)org>
Subject: Re: How can I get the last element out of GROUP BY sets?
Date: 2004-01-19 17:38:53
Message-ID: (view raw, whole thread or download thread mbox)
Lists: pgsql-sql
When grilled further on (Mon, 19 Jan 2004 00:44:30 -0500),
Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> confessed:

> Robert Creager <Robert_Creager(at)LogicalChaos(dot)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
         date_trunc( 'hour', "when" ) AS period,
      FROM readings
      ORDER BY period, "when" DESC) AS p

Thanks for the tip.


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

In response to

pgsql-sql by date

Next:From: Jim JohannsenDate: 2004-01-20 00:13:20
Subject: Re: Is it possible in PostgreSQL?
Previous:From: Tom LaneDate: 2004-01-19 15:07:54
Subject: Re: name of a column returned from a table function

Privacy Policy | About PostgreSQL
Copyright © 1996-2017 The PostgreSQL Global Development Group