尝试使用Postgresql从SELECT语句返回记录时出现问题

时间:2016-07-28 20:09:32

标签: postgresql plpgsql

我对Postgresql很新。我几天前下载了Postgresql的v9.5,发现这个数据库非常具有挑战性,只是试图从一个相当长的SELECT语句中获取我的结果集。我使用的是pgAdmin III产品,并希望我能看到我的结果数据,但无济于事。

我已经继承了这段代码并尝试进行更改,并尝试不使用HARD CODE使用变量的行,如果可以避免的话。

我已经在谷歌上搜索了2天但又无济于事,我尝试了很多不同的版本,但仍然没有做正确的事情。任何帮助/方向将不胜感激。

以下是我遇到的错误:

********** Error **********
ERROR: query has no destination for result data
SQL state: 42601
Hint: If you want to discard the results of a SELECT, use PERFORM instead.
Context: PL/pgSQL function getrecords() line 14 at SQL statement

这是我的代码(对不起查询的长度):

CREATE OR REPLACE FUNCTION getRecords() RETURNS TABLE (
    title TEXT,


Number_of_visits BIGINT,
    Daily_Visit_Total_hours TEXT,
    First_Aid_Visits BIGINT,
    First_Aid_Total_hours TEXT,
    Major_Accident_Visits BIGINT,
    Major_Accident_Total_hours TEXT,
    Illness_Visits BIGINT,
    Illness_Total_hours TEXT,
    Medication_Administration_Visits BIGINT,
    Medication_Administration_Total_hours TEXT,
    Specialization_Visits BIGINT,
    Specialization_Total_hours TEXT,
    Diabetic_Visits BIGINT,
    Diabetic_Total_Hours TEXT) AS $$
#variable_conflict use_variable
DECLARE
    SYEAR_DATE NUMERIC;  
    START_DATE DATE;
    END_DATE DATE; 
    SCHOOL_ID NUMERIC;
BEGIN
    SYEAR_DATE := 2015;
    START_DATE := '2015-08-01';
    END_DATE := '2015-12-31';
    SCHOOL_ID := 002;

    SELECT DISTINCT
        '999 - DISTRICT TOTALS',
        dailyVisitTotal.count as Number_of_visits,
        round (dailyVisitTotal.duration_total / 60, 2) || 'hrs' as Daily_Visit_Total_hours,
        firstAid.count as First_Aid_Visits,
        round (firstAid.duration_total / 60, 2) || 'hrs' as First_Aid_Total_hours,
        majorAcc.count as Major_Accident_Visits,
        round (majorAcc.duration_total / 60, 2) || 'hrs' as Major_Accident_Total_hours,
        illness.count as Illness_Visits,
        round (illness.duration_total / 60, 2) || 'hrs' as Illness_Total_hours,
        medicationAdminTotal.count as Medication_Administration_Visits,
        round (medicationAdminTotal.duration_total / 60, 2) || 'hrs' as Medication_Administration_Total_hours,
        specTotal.count as Specialization_Visits,
        round (specTotal.duration_total / 60, 2) || 'hrs' as Specialization_Total_hours, 
        diabeticTotal.count as Diabetic_Visits,
        round (diabeticTotal.duration_total / 60, 2) || 'hrs' as Diabetic_Total_Hours
    FROM student_enrollment se 
    LEFT JOIN (
        SELECT 
            se.syear, 
            count(*),
            sum(coalesce(log_field20::numeric, 0)) as duration_total 
        FROM custom_field_log_entries sle 
        INNER JOIN student_enrollment se 
            on (sle.source_id=se.student_id 
            and se.custom_9 is null 
            and se.syear=SYEAR_DATE 
            --and se.syear=2015
            and se.end_date is null)
        WHERE 
            --student_field_id = 400000941
            legacy_field_id = 400000941
            and log_field2::date between START_DATE and END_DATE 
            --and log_field2::date between '2015-08-01' and '2015-12-31'
            and (log_field20 ~ '^[0-9]+$' or log_field20 is null) 
        GROUP BY se.syear
    ) dailyVisitTotal 
    on (se.syear = dailyVisitTotal.syear)
    LEFT JOIN (
        SELECT 
            se.syear, 
            count(*),
            sum(coalesce(log_field20::numeric, 0)) as duration_total 
        FROM custom_field_log_entries sle 
        INNER JOIN student_enrollment se 
            on (sle.source_id=se.student_id 
            and se.custom_9 is null 
            and se.syear=SYEAR_DATE 
            --and se.syear=2015
            and se.end_date is null)
        WHERE 
            --student_field_id = 400000941
            legacy_field_id=400000941
            and log_field2::date between START_DATE and END_DATE  
            --and log_field2::date between '2015-08-01' and '2015-12-31'
            and (log_field20 ~ '^[0-9]+$' or log_field20 is null) 
            and log_field4='Y'
        GROUP BY se.syear
    ) firstAid 
    on (se.syear = firstAid.syear)
    LEFT JOIN (
        SELECT 
            se.syear, 
            count(*),
            sum(coalesce(log_field20::numeric, 0)) as duration_total 
        FROM custom_field_log_entries sle 
        INNER JOIN student_enrollment se 
            on (sle.source_id=se.student_id 
            and se.custom_9 is null 
            and se.syear=SYEAR_DATE 
            --and se.syear=2015
            and se.end_date is null)
        WHERE 
            --student_field_id = 400000941
            legacy_field_id=400000941
            and log_field2::date between START_DATE and END_DATE 
            --and log_field2::date between '2015-08-01' and '2015-12-31'
            and (log_field20 ~ '^[0-9]+$' or log_field20 is null)
            and log_field9='Y'
        GROUP BY se.syear
    ) majorAcc 
    on (se.syear = majorAcc.syear)
    LEFT JOIN (
        SELECT 
            se.syear, 
            count(*),
            sum(coalesce(log_field20::numeric, 0)) as duration_total 
        FROM custom_field_log_entries sle 
        INNER JOIN student_enrollment se 
            on (sle.source_id=se.student_id 
            and se.custom_9 is null 
            and se.syear=SYEAR_DATE 
            --and se.syear=2015
            and se.end_date is null)
        WHERE 
            --student_field_id = 400000941
            legacy_field_id=400000941
            and log_field2::date between START_DATE and END_DATE  
            --and log_field2::date between '2015-08-01' and '2015-12-31'
            and (log_field20 ~ '^[0-9]+$' or log_field20 is null) 
            and log_field5='Y'
        GROUP BY se.syear
    ) illness 
    on (se.syear = illness.syear)
    LEFT JOIN (
        SELECT 
            se.syear, 
            count(*),
            sum(coalesce(log_field2::numeric, 0)) as duration_total  
        FROM custom_field_log_entries sle 
        INNER JOIN student_enrollment se 
            on (sle.source_id=se.student_id 
            and se.custom_9 is null 
            and se.syear=SYEAR_DATE 
            --and se.syear=2015
            and se.end_date is null)
        WHERE 
            --student_field_id = 400001237 
            legacy_field_id=400001237
            and log_field5::date between START_DATE and END_DATE
            --and log_field5::date between '2015-08-01' and '2015-12-31'
            and (log_field2 ~ '^[0-9]+$' or log_field2 is null)
        GROUP BY se.syear
    ) medicationAdminTotal 
    on (se.syear = medicationAdminTotal.syear) 
    LEFT JOIN (
        SELECT 
            se.syear, 
            count(*),
            sum(coalesce(log_field11::numeric, 0)) as duration_total 
        FROM custom_field_log_entries sle 
        INNER JOIN student_enrollment se 
            on (sle.source_id=se.student_id 
            and se.custom_9 is null 
            and se.syear=SYEAR_DATE 
            --and se.syear=2015
            and se.end_date is null)
        WHERE 
            --student_field_id = 400009202 
            legacy_field_id=400009202
            and log_field3::date between START_DATE and END_DATE
            --and log_field3::date between '2015-08-01' and '2015-12-31'
            and (log_field11 ~ '^[0-9]+$' or log_field11 is null)
        GROUP BY se.syear
    ) specTotal 
    on (se.syear = specTotal.syear)  
    LEFT JOIN (
        SELECT 
            se.syear, 
            count(*),
            sum(coalesce(log_field14::numeric, 0)) as duration_total 
        FROM custom_field_log_entries sle 
        INNER JOIN student_enrollment se 
            on (sle.source_id=se.student_id 
            and se.custom_9 is null 
            and se.syear=SYEAR_DATE 
            --and se.syear=2015
            and se.end_date is null)
        WHERE 
            --student_field_id = 400009003 
            legacy_field_id=400009003
            and log_field1::date between START_DATE and END_DATE
            --and log_field1::date between '2015-08-01' and '2015-12-31'
            and (log_field14 ~ '^[0-9]+$' or log_field14 is null)
        GROUP BY se.syear
    ) diabeticTotal 
    on (se.syear = diabeticTotal.syear) 
    WHERE 
        se.syear = SYEAR_DATE
        --se.syear = 2015
        and se.end_date is null 
        and se.custom_9 is null 
        and se.syear=SYEAR_DATE 
        --and se.syear=2015
        and se.end_date is null
        and (
            specTotal.duration_total is not null 
            or dailyVisitTotal.duration_total is not null 
            or diabeticTotal.duration_total is not null 
            or medicationAdminTotal.duration_total is not null
        ) 
    ORDER BY 1;
END $$ LANGUAGE 'plpgsql';

SELECT * FROM getRecords();

1 个答案:

答案 0 :(得分:0)

Add RETURN QUERY before your query. That should tell postgres to return the records from your query.

BEGIN
    SYEAR_DATE := 2015;
    START_DATE := '2015-08-01';
    END_DATE := '2015-12-31';
    SCHOOL_ID := 002;

    RETURN QUERY SELECT DISTINCT

    '999 - DISTRICT TOTALS',
    dailyVisitTotal.count as Number_of_visits,
    round (dailyVisitTotal.duration_total / 60, 2) || 'hrs' as Daily_Visit_Total_hours,
    firstAid.count as First_Aid_Visits,
...