Postgres函数

时间:2017-01-06 15:19:52

标签: sql postgresql function syntax

我在创建一个需要2个输入(startDate和endDate)的函数时遇到问题,它将返回这些日期之间的所有结果。我认为语法是我的主要问题。我目前使用的日期仅用于测试目的。我该怎么办?

CREATE OR REPLACE FUNCTION report_new(stDt date, endDt date) 
RETURNS TABLE("Form" integer, "CBID" character varying(4000),"Drug Duration" character varying(4000),"DateNotify" character varying(4000),"Dose" character varying(4000),
"Agent" character varying(4000),"Onset" character varying(4000),"Expectedness" character varying(4000),
"Grade" character varying(4000),"Investigator's opinion" character varying(4000),"Protocol No." character varying(4000), 
"Medical Monitor's Opinion" character varying(4000),"Status" character varying(4000),"Offset" character varying(4000),
"Reported Term" character varying(4000),"Why Serious" character varying(4000),"Study No." character varying(4000),"Subject No." character varying(4000))

AS
$BODY$

SELECT * FROM CROSSTAB(
'SELECT event_crf_id, i.description, id.value FROM item i, item_data id 
WHERE i.item_id=id.item_id 
AND id.item_id IN (select item_id from item_data where event_crf_id  IN   (select event_crf_id from event_crf where crf_version_id=88)
AND name SIMILAR TO ''(SAE_SPONSSTDYNO|SAE_STUDYSUBJNO|SAE_IRBPROTNO|SAE_DRGUNINV|SAE_DOSEEVENT|SAE_GRADE|SAE_REPTERM|SAE_EVENTSTART|SAE_EXPOCCSA|SAE_OUTCOME_DAT|SAE_OUTCOME_STATUS|SAE_MM_OPN_REL|SAE_MONOPSER|SAE_EXP|SAE_RESULTDEATH|SAE_CBID|SAE_DTLTRSNT)'') 
AND ''id.date_created between '''||to_char(stDt, 'YYYY-MM-DD') || ''' AND '''||to_char(endDt, 'YYYY-MM-DD')'''ORDER BY 1,2') 
AS ct ("Form" integer, "CBID" character varying(4000),"Drug Duration" character varying(4000),"DateNotify" character varying(4000),"Dose" character varying(4000),
"Agent" character varying(4000),"Onset" character varying(4000),"Expectedness" character varying(4000),
"Grade" character varying(4000),"Investigator's opinion" character varying(4000),"Protocol No." character varying(4000), 
"Medical Monitor's Opinion" character varying(4000),"Status" character varying(4000),"Offset" character varying(4000),
"Reported Term" character varying(4000),"Why Serious" character varying(4000),"Study No." character varying(4000),"Subject No." character varying(4000))

$BODY$
LANGUAGE 'sql' VOLATILE;

1 个答案:

答案 0 :(得分:0)

我通过实际拆分一切来实现它。创建了一个生成整体数据的函数,然后创建了另一个函数,该函数将调用前一个函数并根据日期选择缩小结果范围,依此类推......

我稍后会用完整的代码更新,也许它会帮助其他人:)