[pgsql-jp: 28346] Re: 年代別の集計

RCS大熊 ohguma @ rnc-com.co.jp
2002年 12月 16日 (月) 20:18:28 JST


おおぐまです。

もっとスマートにできそうですが、こんなのはどうでしょうか?

環境:PostgresSQL 7.3

#create temp table person( birth date, name text);
#insert into person values( '2002-1-1', 'ab' );
#insert into person values( '2000-1-1', 'ba' );
#insert into person values( '1990-1-1', 'cde' );
#insert into person values( '1991-1-1', 'efg' );
#insert into person values( '1992-1-1', 'hi' );
#insert into person values( '1993-1-1', 'jk' );
#insert into person values( '1980-1-1', 'lm' );
#insert into person values( '1985-1-1', 'asd' );
#select
  ((date_part('year',age('now',birth))::integer / 5 ) * 5 ) || '-' ||
  (((date_part('year',age('now',birth))::integer / 5 ) + 1) * 5 - 1 ) as age,
  count(*)
from person
group by (date_part('year',age('now',birth))::integer / 5);

  age  | count
-------+-------
 0-4   |     2
 5-9   |     1
 10-14 |     3
 15-19 |     1
 20-24 |     1



> 石浦です。
> 生年月日から年齢をはじきだし、それを年代別で集計しようとしています。
> 通常の範囲検索であれば
> 例えば20〜29の20代を検索する場合
> select count(*) from member where date_part('year',age('now',birthday))
> between 20 and 29
> で検索ヒットできます。
> 
> 今回は検索ではなく会員分析等で年代別の集計をスマートなSQLでできないもの
> かと考えています。
> つまり
> agetime|count
> -----------------
> ・・・・
> 20-24  |24
> 25-29  |23
> 30-34  |56
> 35-39  |45
> ・・・・
> -------------------
> のように1つのSQLで表現できないものでしょうか。

--
RNC Computer System Co., Ltd.
大熊秀和 (OHGUMA Hidekazu)
E-mail: ohguma @ rnc-com.co.jp
Tel: 087-823-3011 Fax: 087-823-3101



pgsql-jp メーリングリストの案内