Re: interval questions - Mailing list pgsql-general

From Tom Lane
Subject Re: interval questions
Date
Msg-id 9897.959913760@sss.pgh.pa.us
Whole thread Raw
In response to interval questions  (Michael Blakeley <mike@blakeley.com>)
List pgsql-general
Michael Blakeley <mike@blakeley.com> writes:
> 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

You don't say *why* you need DISTINCT ON, or exactly what output you
are hoping to get (presumably not a straight average over all the table
entries) ... but perhaps something like
    SELECT id, avg(age(stamp)) FROM events GROUP BY id;
is what you need?

            regards, tom lane

pgsql-general by date:

Previous
From: Tom Lane
Date:
Subject: Re: query optimiser changes 6.5->7.0
Next
From: Alfred Perlstein
Date:
Subject: Re: interval questions