I have a SQL function called get_forecast_history(integer,integer)
that takes two arguments, a month and a year. The function returns a CUSTOM TYPE created with:
CREATE TYPE fcholder AS (y integer, m integer, product varchar, actual real);
The first line of the function definition is:
CREATE OR REPLACE FUNCTION get_forecast_history(integer, integer)
RETURNS SETOF fcholder AS $$
Calling:
SELECT * FROM get_forecast_history(10, 2011);
For example produces the following table (the result type of the function is a table i.e. SETOF
):
y m product actual ---- -- -------- ------ 2011 10 Product1 29 2011 10 Product2 10 2011 10 Product3 8 2011 10 Product4 0 2011 10 Product5 2
etc. (about 30 products total). This is the history for the given month.
I also have another query that generates a series of months:
SELECT to_char(DATE '2008-01-01'
+ (interval '1 month' * generate_series(0,57)), 'YYYY-MM-DD') AS ym
Which products a list like this:
ym ---------- 2008-01-01 2008-02-01 2008-03-01 2008-04-01 ... 2011-10-01
I need to somehow LEFT JOIN
the results of the generate_series
of year/month combinations on the function above by taking the results of the generate_series
and passing them as arguments to the function. This way I'll get the results of the function, but for every year/month combination from the generate_series
. At this point I'm stuck.
I'm using PostgreSQL 8.3.14.