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

  • SEARCH
  • Home
  • 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 7577453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T17:11:20+00:00 2026-05-30T17:11:20+00:00

Is this possible? I have a function which creates a table. I want a

  • 0

Is this possible? I have a function which creates a table.

I want a view which can call the function then select on the table it creates. It needs to be a view as there is some php graph stuff on top which uses views to display its data.

Something like:

CREATE OR REPLACE VIEW vwi_spm_avg_enroll_rate AS
SELECT * FROM fn_spm_avg_enroll_rate();
SELECT * FROM avg_enroll_rate;

That doesn’t work but I wondered if there was a way or if anyone knew a different way to do this?

Cheers
EDIT: CODE:

set search_path to "[study]", public;


DROP FUNCTION fn_spm_avg_enroll_rate();
CREATE OR REPLACE FUNCTION fn_spm_avg_enroll_rate()
RETURNS VOID
SECURITY DEFINER
AS $proc$
DECLARE
    month1 text;
    month2 text;
    month3 text;
    month4 text;
    month5 text;
    month6 text;
    month7 text;
    month8 text;
    month9 text;
    month10 text;
    month11 text;
    month12 text;

    sqlStr text;
    sqlStr2 text;
    insertStr text;
    r record;

BEGIN 
    select to_char((current_date - interval '1 Month')::date,'Mon-YYYY') INTO month1;
    select to_char((current_date - interval '2 Month')::date,'Mon-YYYY') INTO month2;
    select to_char((current_date - interval '3 Month')::date,'Mon-YYYY') INTO month3;
    select to_char((current_date - interval '4 Month')::date,'Mon-YYYY') INTO month4;
    select to_char((current_date - interval '5 Month')::date,'Mon-YYYY') INTO month5;
    select to_char((current_date - interval '6 Month')::date,'Mon-YYYY') INTO month6;
    select to_char((current_date - interval '7 Month')::date,'Mon-YYYY') INTO month7;
    select to_char((current_date - interval '8 Month')::date,'Mon-YYYY') INTO month8;
    select to_char((current_date - interval '9 Month')::date,'Mon-YYYY') INTO month9;
    select to_char((current_date - interval '10 Month')::date,'Mon-YYYY') INTO month10;
    select to_char((current_date - interval '11 Month')::date,'Mon-YYYY') INTO month11;
    select to_char((current_date - interval '12 Month')::date,'Mon-YYYY') INTO month12;

    sqlStr := $$SELECT country_name,
                SUM( CASE WHEN date_trunc('month', "C1".eldate::date) =  date_trunc('month', now()) - interval '12 months' THEN 1 ELSE 0 END ) AS "month12",
                SUM( CASE WHEN date_trunc('month', "C1".eldate::date) =  date_trunc('month', now()) - interval '11 months' THEN 1 ELSE 0 END ) AS "month11",
                SUM( CASE WHEN date_trunc('month', "C1".eldate::date) =  date_trunc('month', now()) - interval '10 months' THEN 1 ELSE 0 END ) AS "month10",
                SUM( CASE WHEN date_trunc('month', "C1".eldate::date) =  date_trunc('month', now()) - interval '9 months' THEN 1 ELSE 0 END ) AS "month9",
                SUM( CASE WHEN date_trunc('month', "C1".eldate::date) =  date_trunc('month', now()) - interval '8 months' THEN 1 ELSE 0 END ) AS "month8",
                SUM( CASE WHEN date_trunc('month', "C1".eldate::date) =  date_trunc('month', now()) - interval '7 months' THEN 1 ELSE 0 END ) AS "month7",
                SUM( CASE WHEN date_trunc('month', "C1".eldate::date) =  date_trunc('month', now()) - interval '6 months' THEN 1 ELSE 0 END ) AS "month6",
                SUM( CASE WHEN date_trunc('month', "C1".eldate::date) =  date_trunc('month', now()) - interval '5 months' THEN 1 ELSE 0 END ) AS "month5",
                SUM( CASE WHEN date_trunc('month', "C1".eldate::date) =  date_trunc('month', now()) - interval '4 months' THEN 1 ELSE 0 END ) AS "month4",
                SUM( CASE WHEN date_trunc('month', "C1".eldate::date) =  date_trunc('month', now()) - interval '3 months' THEN 1 ELSE 0 END ) AS "month3",
                SUM( CASE WHEN date_trunc('month', "C1".eldate::date) =  date_trunc('month', now()) - interval '2 months' THEN 1 ELSE 0 END ) AS "month2",
                SUM( CASE WHEN date_trunc('month', "C1".eldate::date) =  date_trunc('month', now()) - interval '1 months' THEN 1 ELSE 0 END ) AS "month1"

            FROM country AS c
                INNER JOIN "site" AS s using (country_id)
                INNER JOIN "subject_C1" AS "C1" ON "s"."site_id" = "C1"."site_id"

            GROUP BY 1$$;

EXECUTE $$DROP TABLE avg_enroll_rate CASCADE$$;

EXECUTE $$CREATE TABLE avg_enroll_rate ("__SeriesName" VARCHAR(512), "__VectorX" VARCHAR(512), "__VectorY" INTEGER)$$; 


FOR r IN
        EXECUTE sqlStr
LOOP
        --RAISE NOTICE 'Record: %', r;
        sqlStr2 := 'INSERT INTO "avg_enroll_rate" VALUES ( ' || quote_literal(r.country_name) || ','  || quote_literal(month12) || ',' || r.month12 || ')';
        EXECUTE sqlStr2;
        sqlStr2 := 'INSERT INTO "avg_enroll_rate" VALUES ( ' || quote_literal(r.country_name) || ','  || quote_literal(month11) || ',' || r.month11 || ')';
        EXECUTE sqlStr2;
        sqlStr2 := 'INSERT INTO "avg_enroll_rate" VALUES ( ' || quote_literal(r.country_name) || ','  || quote_literal(month10) || ',' || r.month10 || ')';
        EXECUTE sqlStr2;
        sqlStr2 := 'INSERT INTO "avg_enroll_rate" VALUES ( ' || quote_literal(r.country_name) || ','  || quote_literal(month9) || ',' || r.month9 || ')';
        EXECUTE sqlStr2;
        sqlStr2 := 'INSERT INTO "avg_enroll_rate" VALUES ( ' || quote_literal(r.country_name) || ','  || quote_literal(month8) || ',' || r.month8 || ')';
        EXECUTE sqlStr2;
        sqlStr2 := 'INSERT INTO "avg_enroll_rate" VALUES ( ' || quote_literal(r.country_name) || ','  || quote_literal(month7) || ',' || r.month7 || ')';
        EXECUTE sqlStr2;
        sqlStr2 := 'INSERT INTO "avg_enroll_rate" VALUES ( ' || quote_literal(r.country_name) || ','  || quote_literal(month6) || ',' || r.month6 || ')';
        EXECUTE sqlStr2;
        sqlStr2 := 'INSERT INTO "avg_enroll_rate" VALUES ( ' || quote_literal(r.country_name) || ','  || quote_literal(month5) || ',' || r.month5 || ')';
        EXECUTE sqlStr2;
        sqlStr2 := 'INSERT INTO "avg_enroll_rate" VALUES ( ' || quote_literal(r.country_name) || ','  || quote_literal(month4) || ',' || r.month4 || ')';
        EXECUTE sqlStr2;
        sqlStr2 := 'INSERT INTO "avg_enroll_rate" VALUES ( ' || quote_literal(r.country_name) || ','  || quote_literal(month3) || ',' || r.month3 || ')';
        EXECUTE sqlStr2;
        sqlStr2 := 'INSERT INTO "avg_enroll_rate" VALUES ( ' || quote_literal(r.country_name) || ','  || quote_literal(month2) || ',' || r.month2 || ')';
        EXECUTE sqlStr2;
        sqlStr2 := 'INSERT INTO "avg_enroll_rate" VALUES ( ' || quote_literal(r.country_name) || ','  || quote_literal(month1) || ',' || r.month1 || ')';
        EXECUTE sqlStr2;


END LOOP;

END
$proc$
LANGUAGE 'PLPGSQL';

I’m new to sql so sorry if its rubbish code 🙁

  • 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-30T17:11:22+00:00Added an answer on May 30, 2026 at 5:11 pm

    By looking at your code for the function, you are returning the table avg_enroll_rate instead of doing a SELECT on that table, you can just call your function and it should do the same thing.

    CREATE OR REPLACE VIEW vwi_spm_avg_enroll_rate AS
    SELECT * 
    FROM fn_spm_avg_enroll_rate();
    -- SELECT * FROM avg_enroll_rate; this is unneeded because for function is returning the table
    

    Then when you SELECT from your view you will get the data that you need from the avg_enroll_rate table since you returned it by calling the function.

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

Sidebar

Related Questions

Is this possible? I want to have the To:, Body, and an Attachment all
Is this possible? I have a solution with both projects, I just want to
Is this possible? In an events system an event can have multiple times. (ie,
in sqlite it is possible to have string by which the table was created:
I am learning Haskell. I have created function which returns multiplication table up to
Is this possible to have two (or more) different kinds of cells to be
is this possible to do , or do i have to do it all
Surely this is possible? I have been hunting through PyQt tutorials and documentation but
I have no idea if this is possible, but if it is, what would
I have no idea if this is possible ... but it would be cool.

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.