Re: group by range of values

From: Rodrigo De León <rdeleonp(at)gmail(dot)com>
To: "Carol Cheung" <cacheung(at)consumercontact(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: group by range of values
Date: 2007-07-27 19:31:32
Message-ID: a55915760707271231w15d18836me4c3c4f8685e142a@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On 7/27/07, I wrote:
> On 7/27/07, Carol Cheung <cacheung(at)consumercontact(dot)com> wrote:
> > Something like:
> >
> > decade | average(salary)
> > -------+-----------------
> > 1940 | 69500
> > 1950 | 53333.33
> > 1960 | 53000
> > 1970 | 40333.33
>
> CREATE TABLE tester (
> birth_year integer,
> salary numeric(10,2)
> );
>
> SELECT
> SUBSTRING(TO_CHAR(BIRTH_YEAR,'FM9999') FROM 1 FOR 3)||'0'
> AS DECADE
> , AVG(SALARY) AS AVG_SALARY
> FROM TESTER
> GROUP BY
> SUBSTRING(TO_CHAR(BIRTH_YEAR,'FM9999') FROM 1 FOR 3)||'0'
> ORDER BY DECADE;

A bit simpler, if year is int:

SELECT
BIRTH_YEAR/10*10
AS DECADE
, AVG(SALARY) AS AVG_SALARY
FROM TESTER
GROUP BY
BIRTH_YEAR/10*10
ORDER BY DECADE;

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Michael Glaesemann 2007-07-27 20:43:03 Re: group by range of values
Previous Message Pavel Stehule 2007-07-27 19:30:23 Re: group by range of values