Thread: average/stddev on all values returned by a select distinct

average/stddev on all values returned by a select distinct

From
ann hedley
Date:
I have this query which returns the length of the longest consensus for
each id.

lumbribase=# select distinct on (id) length(consensus) from cluster
order by id,length(consensus) desc;
 length
--------
    647
    680
    273
    384
   1285
<snip>

What I want is the average and stddev of the set of lengths returned by
this query.  Something like...

select average(select distinct on (id) length(consensus) from cluster
order by id,length(consensus) desc);

Could someone tell me how it's done?

Thanks

--
Ann

"In a world without walls and fences - who needs Windows and Gates ?"
                                                           (unknown)


Re: average/stddev on all values returned by a select distinct

From
Michael Fuhr
Date:
On Tue, Jul 17, 2007 at 01:51:21PM +0100, ann hedley wrote:
> What I want is the average and stddev of the set of lengths returned by
> this query.  Something like...
>
> select average(select distinct on (id) length(consensus) from cluster
> order by id,length(consensus) desc);

I think this is what you're looking for:

select avg(length)
  from (
    select distinct on (id) length(consensus)
      from cluster
     order by id, length(consensus) desc
  ) s;

--
Michael Fuhr

Re: average/stddev on all values returned by a select distinct

From
ann hedley
Date:
Michael Fuhr wrote:
> On Tue, Jul 17, 2007 at 01:51:21PM +0100, ann hedley wrote:
>
>> What I want is the average and stddev of the set of lengths returned by
>> this query.  Something like...
>>
>> select average(select distinct on (id) length(consensus) from cluster
>> order by id,length(consensus) desc);
>>
>
> I think this is what you're looking for:
>
> select avg(length)
>   from (
>     select distinct on (id) length(consensus)
>       from cluster
>      order by id, length(consensus) desc
>   ) s;
>
>
Thank you, it was indeed.

--
Ann

"In a world without walls and fences - who needs Windows and Gates ?"
                                                           (unknown)