Re: interval questions

From: Ed Loehr <eloehr(at)austin(dot)rr(dot)com>
To: Michael Blakeley <mike(at)blakeley(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: interval questions
Date: 2000-06-02 03:21:09
Message-ID: 39372825.FF359FF9@austin.rr.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Michael Blakeley wrote:
>
> CREATE TABLE EVENTS( stamp date, id varchar(16), event varchar(128) );
>
> I'm trying to find the average age of the records. I've gotten as far as:
> SELECT DISTINCT ON(id) age(stamp) FROM EVENTS;
>
> Now, I need the DISTINCT ON(id), but that means I can't simply avg() the age:
> ERROR: Attribute events.id must be GROUPed or used in an
> aggregate function
>

Interesting problem. Would this do it?

select into temp_age id, sum(age(stamp)) as age_sum, count(id)
from EVENTS group by id;

followed by

select avg(age_sum/count) from temp_age;

Regards,
Ed Loehr

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Elliot Finley 2000-06-02 03:37:23 Re: btree index and max()
Previous Message Richard Smith 2000-06-02 03:08:48 Re: ALTERING A TABLE