Re: interval questions - Mailing list pgsql-general

From Ed Loehr
Subject Re: interval questions
Date
Msg-id 39372825.FF359FF9@austin.rr.com
Whole thread Raw
In response to interval questions  (Michael Blakeley <mike@blakeley.com>)
Responses Re: interval questions  (Michael Blakeley <mike@blakeley.com>)
List 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

pgsql-general by date:

Previous
From: Richard Smith
Date:
Subject: Re: ALTERING A TABLE
Next
From: Michael Blakeley
Date:
Subject: Re: interval questions