Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6754859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:19:53+00:00 2026-05-26T13:19:53+00:00

I have a SQL function called get_forecast_history(integer,integer) that takes two arguments, a month and

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T13:19:54+00:00Added an answer on May 26, 2026 at 1:19 pm

    What you are trying to to could work like this:

    Edit with additional info

    CREATE OR REPLACE FUNCTION f_products_per_month()
      RETURNS SETOF fcholder AS
    $BODY$
    DECLARE
        r fcholder;
    BEGIN
    
    FOR r.y, r.m IN
        SELECT to_char(x, 'YYYY')::int4  -- AS y
              ,to_char(x, 'MM')::int4    -- AS m
        FROM  (SELECT '2008-01-01 0:0'::timestamp
            + (interval '1 month' * generate_series(0,57)) AS x) x
    LOOP
        RETURN QUERY
        SELECT *    -- use '*' in this case to stay in sync
        FROM   get_forecast_history(r.m, r.y);
    
        IF NOT FOUND THEN
           RETURN NEXT r;
        END IF;
    END LOOP;
    
    END;
    $BODY$
      LANGUAGE plpgsql;
    

    Call:

    SELECT * FROM f_products_per_month();
    

    Major points:

    • Final edit to include an otherwise empty row for months without products.
    • You wrote “LEFT JOIN”, but that’s not how it can work.
    • There are several ways to do this, but RETURN QUERY is the most elegant.
    • Use the same return type as your function get_forecast_history() uses.
    • Avoid naming conflicts with the OUT parameters by table-qualifying the column names (not applicable any more in the final version).
    • Don’t use DATE '2008-01-01', use a timestamp like I did, it has to be converted for to_char() anyway. Less casting, performs better (not that it matters much in this case).
    • '2008-01-01 0:0'::timestamp and timestamp '2008-01-01 0:0' are just two syntax variants doing the same.
    • For older versions of PostgreSQL the language plpgsql is not installed by defualt. You may have to issue CREATE LANGUAGE plpgsql; once in your database. See the manual here.

    You could probably simplify your two functions into one query or function if you wanted.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an MS SQL function that is called with the following syntax: SELECT
I have a user defined function in SQL called getBuisnessDays it takes @startdate and
I have a function in SQL server 2008 that takes a string: 'A,B,C,D' and
I have created a function that takes a SQL command and produces output that
I have an user defined table function in SQL Server that aggregate data from
I have two T-SQL scalar functions that both perform calculations over large sums of
I have a PL/SQL function (running on Oracle 10g) in which I update some
I have a huge problem with the REPLACE SQL function in Microsoft SQL Server
I'm currently debugging an Ms SQL Function (SQL 2008). In this function, I have
We have a Single Statement FUNCTION in SQL Server 2005 which uses CONTAINSTABLE(). All

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.