2013-06-28 68 views
2

我需要生成一系列的宿舍,給定開始日期和結束日期。我知道generate_series(),但它只是不quarter工作:在Postgresql中生成一系列宿舍

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; 

回答

7

不應該做這你所期望的:

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 rows) 
+0

OK,我是盲人:)它的工作原理,但它應該爲期3個月。 –

+0

啊,當然:)回答編輯了3個月 –