Hi i have a Table input step that declares a function in postgres sql
and then i execute SELECT * FROM somefuncname(); to run my function which returns a table
When i run this in pgadmin it works fine but when i try to execute it inside the table input step i get the error mentioned in the title of the thead.
The whole sql statement is this
Any ideas why this happens?
I'm relatively new to data warehousing
and then i execute SELECT * FROM somefuncname(); to run my function which returns a table
When i run this in pgadmin it works fine but when i try to execute it inside the table input step i get the error mentioned in the title of the thead.
The whole sql statement is this
Code:
CREATE OR REPLACE FUNCTION somefuncname() RETURNS table(type character varying(255),count_after_sales_open numeric,
count_after_sales_closed numeric,
count_application_fu_open numeric,
count_application_fu_closed numeric,
count_campaign_open numeric,
count_campaign_closed numeric,
count_error_open numeric,
count_error_closed numeric,
count_cancellation_open numeric,
count_cancellation_closed numeric,
count_renewal_open numeric,
count_renewal_closed numeric,
count_sales_open numeric,
count_sales_closed numeric,
count_service_open numeric,
count_service_closed numeric,
count_claims_open numeric,
count_claims_closed numeric,
count_complain_open numeric,
count_complain_closed numeric,
count_endorsment_open numeric,
count_endorsment_closed numeric,
count_total_open numeric,
count_total_closed numeric) LANGUAGE plpgsql AS $$
declare AsOfDate date = '2016-01-01';
EndDate date = '2016-07-20';
BEGIN
WHILE (AsOfDate < EndDate) LOOP
return query SELECT stats.type,(SELECT sum(COUNT_NUMBER) as count1 FROM DWH_CALL_REQUEST_COMMENT_STATISTICS WHERE date_part('year',AsOfDate)=date_year AND date_part('month',AsOfDate)=(date_month+1) AND date_part('day',AsOfDate)=date_day AND category='After Sales' AND CR_STATUS IN ('CREATE','MISSED') AND cr_type = stats.type)as count_after_sales_open
,(SELECT sum(COUNT_NUMBER) as count1 FROM DWH_CALL_REQUEST_COMMENT_STATISTICS WHERE date_part('year',AsOfDate)=date_year AND date_part('month',AsOfDate)=(date_month+1) AND date_part('day',AsOfDate)=date_day AND category='After Sales' AND CR_STATUS IN ('CLOSED','CANCEL') AND cr_type = stats.type)as count_after_sales_closed
,(SELECT sum(COUNT_NUMBER) as count2 FROM DWH_CALL_REQUEST_COMMENT_STATISTICS WHERE date_part('year',AsOfDate)=date_year AND date_part('month',AsOfDate)=(date_month+1) AND date_part('day',AsOfDate)=date_day AND category='Application follow ups' AND CR_STATUS IN ('CREATE','MISSED') AND cr_type = stats.type) as count_application_fu_open
,(SELECT sum(COUNT_NUMBER) as count2 FROM DWH_CALL_REQUEST_COMMENT_STATISTICS WHERE date_part('year',AsOfDate)=date_year AND date_part('month',AsOfDate)=(date_month+1) AND date_part('day',AsOfDate)=date_day AND category='Application follow ups' AND CR_STATUS IN ('CLOSED','CANCEL') AND cr_type = stats.type) as count_application_fu_closed
,(SELECT sum(count_number) as count3 FROM DWH_CALL_REQUEST_COMMENT_STATISTICS WHERE date_part('year',AsOfDate)=date_year AND date_part('month',AsOfDate)=(date_month+1) AND date_part('day',AsOfDate)=date_day AND category='Campaign' AND CR_STATUS IN ('CREATE','MISSED') AND cr_type = stats.type) as count_campaign_open
,(SELECT sum(count_number) as count3 FROM DWH_CALL_REQUEST_COMMENT_STATISTICS WHERE date_part('year',AsOfDate)=date_year AND date_part('month',AsOfDate)=(date_month+1) AND date_part('day',AsOfDate)=date_day AND category='Campaign' AND CR_STATUS IN ('CLOSED','CANCEL') AND cr_type = stats.type) as count_campaign_closed
,(SELECT sum(count_number) as count3 FROM DWH_CALL_REQUEST_COMMENT_STATISTICS WHERE date_part('year',AsOfDate)=date_year AND date_part('month',AsOfDate)=(date_month+1) AND date_part('day',AsOfDate)=date_day AND category='Error' AND CR_STATUS IN ('CREATE','MISSED') AND cr_type = stats.type) as count_error_open
,(SELECT sum(count_number) as count3 FROM DWH_CALL_REQUEST_COMMENT_STATISTICS WHERE date_part('year',AsOfDate)=date_year AND date_part('month',AsOfDate)=(date_month+1) AND date_part('day',AsOfDate)=date_day AND category='Error' AND CR_STATUS IN ('CLOSED','CANCEL') AND cr_type = stats.type) as count_error_closed
,(SELECT sum(count_number) as count3 FROM DWH_CALL_REQUEST_COMMENT_STATISTICS WHERE date_part('year',AsOfDate)=date_year AND date_part('month',AsOfDate)=(date_month+1) AND date_part('day',AsOfDate)=date_day AND category='Cancellation' AND CR_STATUS IN ('CREATE','MISSED') AND cr_type = stats.type) as count_cancellation_open
,(SELECT sum(count_number) as count3 FROM DWH_CALL_REQUEST_COMMENT_STATISTICS WHERE date_part('year',AsOfDate)=date_year AND date_part('month',AsOfDate)=(date_month+1) AND date_part('day',AsOfDate)=date_day AND category='Cancellation' AND CR_STATUS IN ('CLOSED','CANCEL') AND cr_type = stats.type) as count_cancellation_closed
,(SELECT sum(count_number) as count3 FROM DWH_CALL_REQUEST_COMMENT_STATISTICS WHERE date_part('year',AsOfDate)=date_year AND date_part('month',AsOfDate)=(date_month+1) AND date_part('day',AsOfDate)=date_day AND category='Renewal' AND CR_STATUS IN ('CREATE','MISSED') AND cr_type = stats.type) as count_renewal_open
,(SELECT sum(count_number) as count3 FROM DWH_CALL_REQUEST_COMMENT_STATISTICS WHERE date_part('year',AsOfDate)=date_year AND date_part('month',AsOfDate)=(date_month+1) AND date_part('day',AsOfDate)=date_day AND category='Renewal' AND CR_STATUS IN ('CLOSED','CANCEL') AND cr_type = stats.type) as count_renewal_closed
,(SELECT sum(count_number) as count3 FROM DWH_CALL_REQUEST_COMMENT_STATISTICS WHERE date_part('year',AsOfDate)=date_year AND date_part('month',AsOfDate)=(date_month+1) AND date_part('day',AsOfDate)=date_day AND category='Sales' AND CR_STATUS IN ('CREATE','MISSED') AND cr_type = stats.type) as count_sales_open
,(SELECT sum(count_number) as count3 FROM DWH_CALL_REQUEST_COMMENT_STATISTICS WHERE date_part('year',AsOfDate)=date_year AND date_part('month',AsOfDate)=(date_month+1) AND date_part('day',AsOfDate)=date_day AND category='Sales' AND CR_STATUS IN ('CLOSED','CANCEL') AND cr_type = stats.type) as count_sales_closed
,(SELECT sum(count_number) as count3 FROM DWH_CALL_REQUEST_COMMENT_STATISTICS WHERE date_part('year',AsOfDate)=date_year AND date_part('month',AsOfDate)=(date_month+1) AND date_part('day',AsOfDate)=date_day AND category='Service' AND CR_STATUS IN ('CREATE','MISSED') AND cr_type = stats.type) as count_service_open
,(SELECT sum(count_number) as count3 FROM DWH_CALL_REQUEST_COMMENT_STATISTICS WHERE date_part('year',AsOfDate)=date_year AND date_part('month',AsOfDate)=(date_month+1) AND date_part('day',AsOfDate)=date_day AND category='Service' AND CR_STATUS IN ('CLOSED','CANCEL') AND cr_type = stats.type) as count_service_closed
,(SELECT sum(count_number) as count9 FROM DWH_CALL_REQUEST_COMMENT_STATISTICS WHERE date_part('year',AsOfDate)=date_year AND date_part('month',AsOfDate)=(date_month+1) AND date_part('day',AsOfDate)=date_day AND category='Claims' AND CR_STATUS IN ('CREATE','MISSED') AND cr_type = stats.type)as count_claims_open
,(SELECT sum(count_number) as count9 FROM DWH_CALL_REQUEST_COMMENT_STATISTICS WHERE date_part('year',AsOfDate)=date_year AND date_part('month',AsOfDate)=(date_month+1) AND date_part('day',AsOfDate)=date_day AND category='Claims' AND CR_STATUS IN ('CLOSED','CANCEL') AND cr_type = stats.type)as count_claims_closed
,(SELECT sum(count_number) as count11 FROM DWH_CALL_REQUEST_COMMENT_STATISTICS WHERE date_part('year',AsOfDate)=date_year AND date_part('month',AsOfDate)=(date_month+1) AND date_part('day',AsOfDate)=date_day AND category='Traditional ' AND CR_STATUS IN ('CREATE','MISSED') AND cr_type = stats.type)as count_complain_open
,(SELECT sum(count_number) as count11 FROM DWH_CALL_REQUEST_COMMENT_STATISTICS WHERE date_part('year',AsOfDate)=date_year AND date_part('month',AsOfDate)=(date_month+1) AND date_part('day',AsOfDate)=date_day AND category='Traditional ' AND CR_STATUS IN ('CLOSED','CANCEL') AND cr_type = stats.type)as count_complain_closed
,(SELECT sum(count_number) as count11 FROM DWH_CALL_REQUEST_COMMENT_STATISTICS WHERE date_part('year',AsOfDate)=date_year AND date_part('month',AsOfDate)=(date_month+1) AND date_part('day',AsOfDate)=date_day AND category='Endorsment' AND CR_STATUS IN ('CREATE','MISSED') AND cr_type = stats.type)as count_endorsment_open
,(SELECT sum(count_number) as count11 FROM DWH_CALL_REQUEST_COMMENT_STATISTICS WHERE date_part('year',AsOfDate)=date_year AND date_part('month',AsOfDate)=(date_month+1) AND date_part('day',AsOfDate)=date_day AND category='Endorsment' AND CR_STATUS IN ('CLOSED','CANCEL') AND cr_type = stats.type)as count_endorsment_closed
,(SELECT sum(count_number) as count11 FROM DWH_CALL_REQUEST_COMMENT_STATISTICS WHERE date_part('year',AsOfDate)=date_year AND date_part('month',AsOfDate)=(date_month+1) AND date_part('day',AsOfDate)=date_day AND CR_STATUS IN ('CREATE','MISSED') AND cr_type = stats.type) as count_total_open
,(SELECT sum(count_number) as count11 FROM DWH_CALL_REQUEST_COMMENT_STATISTICS WHERE date_part('year',AsOfDate)=date_year AND date_part('month',AsOfDate)=(date_month+1) AND date_part('day',AsOfDate)=date_day AND CR_STATUS IN ('CLOSED','CANCEL') AND cr_type = stats.type)as count_total_closed
FROM DWH_CALL_REQUEST_STATISTICS as stats
GROUP BY stats.type
ORDER BY case stats.type when 'NT' then 1
when 'NM' then 2
when 'FT' then 3
when 'FM' then 4
end;
AsOfDate = AsOfDate + cast('1 day' as interval);
END LOOP;
END $$;
SELECT * FROM somefuncname();
I'm relatively new to data warehousing