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