I'm trying to generate a series in PostgreSQL with the generate_series function. I need a series of months starting from Jan 2008 until current month + 12
(a year out). I'm using and restricted to PostgreSQL 8.3.14 (so I don't have the timestamp series options in 8.4).
I know how to get a series of days like:
select generate_series(0,365) + date '2008-01-01'
But I am not sure how to do months.
You can interval
generate_series
like this:Which would result in:
Well, if you only need months, you could do:
and just parse that into a date string...
But since you know you'll end up with months 1,2,..,12, why not just go with
select generate_series(1,12);
?You can interval generate_series like this:
Which would result in:
You can also join in other tables this way:
Edit
If you need to calculate the number dynamically, the following could help:
This calculates the number of months since 2008-01-01 and then adds 12 on top of it.
But I agree with Scott: you should put this into a set returning function, so that you can do something like
select * from calc_months(DATE '2008-01-01')