循环通过postgres表并返回wekid

时间:2014-04-25 19:04:57

标签: sql postgresql plpgsql

我要求从2010年开始循环遍历所有报告期的表格。 我需要循环遍历这个表,以便当我传递日期字符串时,如果它小于reporting_periods_o并且返回reporting_period_string,它将接受并比较它。

CREATE OR REPLACE FUNCTION rpt.reportingperiods(IN v_date text)
 RETURNS SETOF text AS
 $BODY$
 DECLARE

v_in_date INT := CAST(v_date AS INT);
i     INT;
v_sk       INT;

BEGIN
For i IN SELECT rpt_sk FROM rpt.REPORTING_PERIODS LOOP 
    SELECT rpt_sk INTO v_sk FROM rpt.REPORTING_PERIODS  where rpt_sk = i;
    IF v_in_date <= (SELECT reporting_periods_o FROM rpt.REPORTING_PERIODS
               WHERE rpt_sk = i) 
    THEN RETURN QUERY SELECT MIN(reporting_periods)reporting_periods FROM rpt.REPORTING_PERIODS WHERE rpt_sk =  v_sk AND CAST(reporting_periods AS DATE) > CAST(now() AS DATE);
    END IF;
END LOOP;

END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100
ROWS 1000;

我的报告期有三个字段:

 rpt_sk, reporting_periods, reporting_periods_o
  1          2014-03-08          20140308

我已经实现了一个函数但是当我执行时我得到了多条记录,我需要能够找回一条记录。

0 个答案:

没有答案