这可能吗?我有一个创建表的功能。
我想要一个可以调用该函数的视图,然后在它创建的表上选择。它需要是一个视图,因为顶部有一些php图形内容,它使用视图来显示其数据。
类似的东西:
CREATE OR REPLACE VIEW vwi_spm_avg_enroll_rate AS
SELECT * FROM fn_spm_avg_enroll_rate();
SELECT * FROM avg_enroll_rate;
这不行,但我想知道是否有办法,或者是否有人知道不同的方法来做这件事?
干杯 编辑:代码:
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';
我是sql的新手很抱歉,如果它的垃圾代码:(
答案 0 :(得分:0)
通过查看函数的代码,您将返回表avg_enroll_rate
而不是在该表上执行SELECT
,您可以调用您的函数,它应该执行相同的操作。< / p>
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
然后当您从视图中SELECT
时,您将从avg_enroll_rate
表中获取所需的数据,因为您通过调用该函数返回了该数据。