pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Masaru Sugawara <rk73@echna.ne.jp>
To: pgsql-sql@postgresql.org
Subject: Re: MEDIAN as custom aggregate?
Date: Sun, 14 Oct 2001 00:19:56 +0900
Message-ID: <20011014001101.2BE2.RK73@echna.ne.jp> (raw)
In-Reply-To: <web-146875@davinci.ethosmedia.com>
References: <web-146875@davinci.ethosmedia.com>

On Fri, 12 Oct 2001 12:38:12 -0700
"Josh Berkus" wrote:

> For those whose stats terminology is rusty, the "median" is the "middle"
> value in a distribution.  For example, if we had the following data:
> 
> Table ages
> person		age
> Jim		21
> Rusty		24
> Carol		37
> Bob		62
> Leah		78
> 
> Our Median would be Carol's age, 37.  This is a different figure from
> the Mean, or Average, which is 44.4.  Using the combination of the Mean
> and the Median you can do all kinds of interesting statistical analysis.
> 

In such case of this, there needs "nextval" in a query to deal with
a large number of rows. I think the following query, is not smart, 
will return the necessary rows (or an average of the rows). But even
so it may need considerable time...


-- (on 7.1.3)
create sequence seq_ages start 1;

select a1.age, a1.rank   -- or select avg(a1.age)
  from (select a0.person, a0.age, (nextval('seq_ages') - 1) as rank
          from (select *, setval('seq_ages',1)   -- to reset a sequence
                  from ages
                  order by age   -- this insignificant "order by" is
                                 --  needed in order to work "setval"
                ) as a0
           order by a0.age
        ) as a1
  where exists (select * from ages
                   where a1.rank >= (select (count(*)+1)/2 from ages)
                     and a1.rank <= (select  count(*)/2+1  from ages)
                )
;



Regards,
Masaru Sugawara




view thread (6+ messages)  latest in thread

Message-ID: <20011014001101.2BE2.RK73@echna.ne.jp>
Permalink:  ../20011014001101.2BE2.RK73@echna.ne.jp/
Also on:    postgresql.org/message-id/20011014001101.2BE2.RK73@echna.ne.jp

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: rk73@echna.ne.jp
  Subject: Re: MEDIAN as custom aggregate?
  In-Reply-To: <20011014001101.2BE2.RK73@echna.ne.jp>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox