-1

[代码]

CREATE OR REPLACE FUNCTION
DWH_PRODUCTION_SUM_QTDYTD(V_TBL_NAME IN VARCHAR2,V_COL_NAM IN VARCHAR2,V_RPRT_DATE VARCHAR2,
V_FLTRCOL IN VARCHAR2,VALUE_OF IN VARCHAR2, V_GROUP_COL VARCHAR2,V_DF_COL VARCHAR2)-----


RETURN NUMBER  IS 

   A_RET_VALUE NUMBER;


V_REPORT_DATE DATE:=V_RPRT_DATE;
V_FILTERCOL VARCHAR2(30):=UPPER(V_FLTRCOL);
V_COL_NAME VARCHAR2(30):=UPPER(V_COL_NAM);
V_TAB_NAME VARCHAR2(30):=UPPER(V_TBL_NAME);
V_VALUE_TYPE VARCHAR2(8):=UPPER(VALUE_OF);
V_GRP_COL VARCHAR2(30):=UPPER(V_GROUP_COL);
V_DIFF_COL VARCHAR2(30):=UPPER(V_DF_COL);
V_QUARTER_NUMBER  NUMBER(1);
V_QTY VARCHAR2(1000);
V_CURRQTD_STRT VARCHAR2(11);
V_CURRQTD_END  VARCHAR2(11);
V_CURRQTD VARCHAR2(1000);
  BEGIN

--------------------GETTING QUARTER NO.------------------------------

V_QTY:='SELECT TO_NUMBER(TO_CHAR(TO_DATE('''||V_REPORT_DATE||'''), ''Q'')) FROM DUAL';
--DBMS_OUTPUT.PUT_LINE(V_QTY);
EXECUTE IMMEDIATE V_QTY INTO V_QUARTER_NUMBER ; 
--DBMS_OUTPUT.PUT_LINE('QT NUM'||V_QUARTER_NUMBER);

--------------------CURRENT QUARTER VALUE------------------------------

SELECT   MIN (t)"CURNT_QTR_ST_DT", MAX (LAST_DAY (t))"CURNT_QTR_END_DT", ADD_MONTHS(MIN (t),-
3)"PREV_QTR_ST_DT", ADD_MONTHS(MAX (LAST_DAY (t)),-3) "PREV_QTR_END_DT" INTO  FROM (SELECT
V_CURRQTD_STRT ,V_CURRQTD_END,V_PRVQTD_STRT, V_PRVQTD_END
 ADD_MONTHS (TRUNC(TO_DATE(TO_DATE(V_REPORT_DATE)), 'YYYY'), LEVEL - 1)  t,
  TO_CHAR (ADD_MONTHS (TRUNC(TO_DATE(TO_DATE(V_REPORT_DATE)), 'YYYY'), LEVEL - 1), 'Q')QTD

 FROM DUAL
   CONNECT BY LEVEL <= 12) A
   WHERE A.QTD  = V_QUARTER_NUMBER;

V_CURRQTD:='SELECT '||V_GRP_COL||', '||V_DIFF_COL||', SUM('||V_COL_NAME||')   AND  TO_DATE-------     ------here m calculating the values dynamically
FROM  '||V_TAB_NAME|| ' WHERE '||V_FILTERCOL||' BETWEEN   TO_DATE('''||V_CURRQTD_STRT||''') ' 
|| '  ('''||V_REPORT_DATE||''') GROUP BY  '||V_GRP_COL||','||V_DIFF_COL||'';

--V_CURRQTD:= V_CURRQTD ||' GROUP BY '||V_GRP_COL;
--DBMS_OUTPUT.PUT_LINE(V_CURRQTD);
--EXECUTE IMMEDIATE V_CURRQTD INTO V_SUM;
EXECUTE IMMEDIATE V_CURRQTD BULK COLLECT INTO CRQNUMS,CRQNUMS1,CRQNUMS2;
FOR I IN CRQNUMS.FIRST .. CRQNUMS.LAST LOOP
DBMS_OUTPUT.PUT_LINE (
' ITEM_CODE''              '       || CRQNUMS(I) || ': '                     || CRQNUMS1(I)  
||         ': '                          || CRQNUMS2(I));
END LOOP;[/code] 

这我需要在表函数中实现....在上面的查询中,m 从第一个 qry 获取 qtr no,然后在第二个获取 qtr 的第一个和最后一个日期...之后 m 动态地将值填充到枚举中。 ... enums2 使用批量收集,但无法以表格形式获取输出...。

我已经实现了这个并获得了正确的值,但需要在表函数中实现以获取 tablur 形式的输出

4

1 回答 1

0

有关如何将结果集转换为表类型的示例。即只返回一个数组(月,qtd):

SQL> create type date_typ as object
  2  (
  3    mon   date,
  4    qtd   integer
  5  )
  6  ;
  7  /

Type created.

SQL>
SQL> create type date_tab as table of date_typ;
  2  /

Type created.

SQL>
SQL>
SQL> create function foo(p_report_date date) return date_tab
  2  is
  3    t_quarter date_tab;
  4  begin
  5
  6    select cast(
  7             multiset(select add_months (trunc(sysdate, 'YYYY'), level - 1) t,
  8                             to_char (add_months (trunc(sysdate, 'YYYY'), level - 1), 'Q')QTD
  9                        from dual
 10                      connect by level <= 12)
 11                as date_tab)
 12      into t_quarter
 13      from dual;
 14    return t_quarter;
 15  end;
 16  /

Function created.

SQL> select foo(sysdate) from dual;

FOO(SYSDATE)(MON, QTD)
--------------------------------------------------------------------------------
DATE_TAB(DATE_TYP('01-01-2012', 1), DATE_TYP('01-02-2012', 1), DATE_TYP('01-03-2
012', 1), DATE_TYP('01-04-2012', 2), DATE_TYP('01-05-2012', 2), DATE_TYP('01-06-
2012', 2), DATE_TYP('01-07-2012', 3), DATE_TYP('01-08-2012', 3), DATE_TYP('01-09
-2012', 3), DATE_TYP('01-10-2012', 4), DATE_TYP('01-11-2012', 4), DATE_TYP('01-1
2-2012', 4))


SQL>
于 2012-12-31T12:18:20.387 回答