8

開始日と終了日を指定して、一連の四半期を生成する必要があります。私は知ってgenerate_series()いますが、それはうまくいきませんquarter

SELECT * FROM generate_series('2008-01-01 00:00'::timestamp,'2009-01-01 12:00', '1 quarter');

この四半期が特別な理由は何ですか? それ以外の場合generate_series()は、マイクロ秒からミレニアムまで、ほとんどすべてで機能します。

select * from generate_series('2008-01-01 00:00'::timestamp,'2008-01-01 00:00:00.001', '1 microsecond');
select * from generate_series('2008-01-01 00:00'::timestamp,'2008-01-01 00:01', '1 second');
select * from generate_series('2008-01-01 00:00'::timestamp,'2008-01-01 01:00', '1 minute');
select * from generate_series('2008-01-01 00:00'::timestamp,'2008-01-01 12:00', '1 hour');
select * from generate_series('2008-01-01 00:00'::timestamp,'2009-01-01 12:00', '1 day');
select * from generate_series('2008-01-01 00:00'::timestamp,'2009-01-01 12:00', '1 week');
select * from generate_series('2008-01-01 00:00'::timestamp,'2009-01-01 12:00', '1 month');
select * from generate_series('2008-01-01 00:00'::timestamp,'2009-01-01 12:00', '1 year');
select * from generate_series('2008-01-01 00:00'::timestamp,'2009-01-01 12:00', '1 decade');
select * from generate_series('2008-01-01 00:00'::timestamp,'2009-01-01 12:00', '1 century');
select * from generate_series('2008-01-01 00:00'::timestamp,'2009-01-01 12:00', '1 millennium');

quarter使用できない場合、他の最適なオプションは何ですか? 今のところ、私は持っています:

select 
   date_trunc('quarter',generate_series) gs 
from 
   generate_series('2008-01-01 00:00'::timestamp,'2009-01-01 12:00', '1 month') 
group by 
   gs
order by
   gs;
4

1 に答える 1

14

これはあなたが期待することをすべきではありません:

SELECT * FROM generate_series('2008-01-01 00:00'::timestamp,
                              '2009-01-01 12:00', '3 months');

結果:

   generate_series   
----------------------
 2008-01-01 00:00:00
 2008-04-01 00:00:00
 2008-07-01 00:00:00
 2008-10-01 00:00:00
 2009-01-01 00:00:00
(5行)
于 2013-06-28T13:05:30.343 に答える