2011-05-19 104 views
0

我的查詢有太多的子查詢,每個查詢都有重複的參數。如何在eclipse中設計報表。這是我的查詢eclipse中帶有子報表過濾器的Birt報表設計

SELECT 
C.COMP_CODE,C.MATCODE,C.ATTRIB1,C.ATTRIB2,C.MAT_NAME,C.SUP_PROD_CODE, 
C.SUP_CODE,C.BRAND_CODE,C.CAT_CODE,SGRPCODE,SUB_SGRPCODE,C.UNIT_CODE, 
NVL(SUM(D.SALES_QTY),0)SALES_QTY, 
NVL(SUM(D.SALES_VAL),0) SALES_VAL, 
MAX(COST_PRICE) GRN_COST_PRICE,GRN_DATE,'sales qty' a, 'sales val' b,'stock' c,'stock val' d 
FROM 

     (
     SELECT A.COMP_CODE,A.MATCODE,B.UNIT_CODE,A.ATTRIB1,A.ATTRIB2,MAT_NAME,SUP_PROD_CODE, 
     SUP_CODE,BRAND_CODE,CAT_CODE,SGRPCODE,SUB_SGRPCODE,COST_PRICE,GRN_DATE FROM 

       (
       SELECT A.COMP_CODE,A.MATCODE,A.ATTRIB1,A.ATTRIB2,MAT_NAME,SUP_PROD_CODE, 
       SUP_CODE,BRAND_CODE,CAT_CODE,SGRPCODE,SUB_SGRPCODE,B.COST_PRICE,B.GRN_DATE FROM 

         (
         SELECT A.COMP_CODE,A.MATCODE,A.ATTRIB1,A.ATTRIB2,B.MAT_NAME,B.SUP_PROD_CODE, 
         B.SUP_CODE,B.BRAND_CODE,B.CAT_CODE,B.SGRPCODE,B.SUB_SGRPCODE FROM 
         MAT_LIST A, 
         MATERIAL_MASTER B 
         WHERE A.COMP_CODE=B.COMP_CODE 
         AND A.MATCODE=B.MATCODE 
         --AND A.MATCODE='168847' 
         )A, 

         (SELECT A.COMP_CODE,A.MAIN_CODE,A.MATCODE,NVL(A.ATTRIB_CODE1,0) ATTRIB1,NVL(A.ATTRIB_CODE2,0) ATTRIB2, 
         A.MAT_TYPE,MAX(A.MAT_COST) COST_PRICE,GRN_DATE 
         FROM INV_GRN_DTL_V A 
         WHERE a.grn_date=(select max(b.grn_date) from inv_grn_dtl_v b 
           where b.comp_code=a.comp_code and 
           b.main_code=a.main_code and 
           b.matcode=a.matcode and 
           nvl(b.grn_status,'P')='A' and 
           nvl(b.auth_status,'P')='A' and 
           b.supcode<>'GDS1' and 
           b.grn_date<=:TO_DT)  
         AND NVL(A.GRN_STATUS,'P')='A' 
         AND NVL(A.AUTH_STATUS,'P')='A' 
         GROUP BY A.COMP_CODE,A.MAIN_CODE,A.MATCODE,A.ATTRIB_CODE1,A.ATTRIB_CODE2,A.MAT_TYPE,GRN_DATE 
         ) B 
       WHERE A.COMP_CODE=B.COMP_CODE(+) 
       AND A.MATCODE=B.MATCODE(+) 
       AND A.ATTRIB1=B.ATTRIB1(+) 
       AND A.ATTRIB2=B.ATTRIB2(+) 
       AND A.COMP_CODE=:P_COMP_CODE) 

       A,(
       SELECT COMP_CODE,MAIN_CODE,UNIT_CODE 
       FROM UNIT_MST WHERE COMP_CODE=56 
       AND UNIT_CODE IN (SELECT DISTINCT UNIT_CODE FROM STK_SALES_VU 
       WHERE ORD_DATE BETWEEN :FR_DT AND :TO_DT 
       AND COMP_CODE=:P_COMP_CODE) 
       --UNION ALL 
       --SELECT DISTINCT COMP_CODE,MAIN_CODE,'STOCK' FROM UNIT_MST WHERE COMP_CODE=:P_COMP_CODE 
       ) B 

     WHERE A.COMP_CODE=B.COMP_CODE 
     AND A.COMP_CODE=:P_COMP_CODE 
     AND UNIT_CODE=DECODE(:P_UNIT_CODE,'ALL',UNIT_CODE,:P_UNIT_CODE) 
     AND CAT_CODE BETWEEN DECODE(:FR_CAT,'ALL',CAT_CODE,:FR_CAT) 
     AND DECODE(:TO_CAT,'ALL',CAT_CODE,:TO_CAT) 
     AND SUP_CODE=DECODE(:P_SUP_CODE,'ALL',SUP_CODE,:P_SUP_CODE)) 

     C,(
     SELECT COMP_CODE,MAIN_CODE,UNIT_CODE,MAT_TYPE,MATCODE,NVL(ATTRIB_CODE1,0) ATTRIB_CODE1,NVL(ATTRIB_CODE2,0) ATTRIB_CODE2, 
     NVL(SUM(SALES_QTY),0) SALES_QTY, SUM(COST_VAL) SALES_VAL 
     FROM 
     (

       SELECT COMP_CODE,MAIN_CODE,UNIT_CODE,MAT_TYPE,MATCODE,NVL(B.ATTRIB_CODE1,0) ATTRIB_CODE1,NVL(B.ATTRIB_CODE2,0) ATTRIB_CODE2, 
       NVL(SUM(B.SALE_QTY),0) SALES_QTY, SUM(B.VAL) COST_VAL 
       FROM STK_SALES_VU_ATT B 
       WHERE ORD_DATE BETWEEN :FR_DT AND :TO_DT 
       AND UNIT_CODE=DECODE(:P_UNIT_CODE,'ALL',UNIT_CODE,:P_UNIT_CODE) 
       AND COMP_CODE=:P_COMP_CODE 
       GROUP BY COMP_CODE,MAIN_CODE,UNIT_CODE,MAT_TYPE,MATCODE,NVL(B.ATTRIB_CODE1,0),NVL(B.ATTRIB_CODE2,0) 

       UNION ALL 

       SELECT COMP_CODE,MAIN_CODE,'STOCK' UNIT_CODE,MAT_TYPE,MATCODE,NVL(ATTRIB_CODE1,0),NVL(ATTRIB_CODE2,0),SUM(INC_QTY)-SUM(DEC_QTY) OB_QTY, 0 SALES_VAL 
       FROM INV_TRN_DAY_SUM_VU_ATT 
       WHERE TRN_DATE BETWEEN :FR_DT AND :TO_DT 
       AND UNIT_CODE=DECODE(:P_UNIT_CODE,'ALL',UNIT_CODE,:P_UNIT_CODE) 
       AND COMP_CODE=:P_COMP_CODE 
       GROUP BY COMP_CODE,MAIN_CODE,MAT_TYPE,MATCODE,NVL(ATTRIB_CODE1,0),NVL(ATTRIB_CODE2,0) 

       UNION ALL 

       SELECT COMP_CODE,MAIN_CODE,'STOCK' UNIT_CODE,MAT_TYPE,MATCODE,NVL(ATTRIB_CODE1,0),NVL(ATTRIB_CODE2,0),SUM(QTY)QTY, 0 SALES_VAL 
       FROM MATERIAL_DETAIL 
       WHERE SERIAL=:P_FNYR 
       AND UNIT_CODE=DECODE(:P_UNIT_CODE,'ALL',UNIT_CODE,:P_UNIT_CODE) 
       AND COMP_CODE=:P_COMP_CODE 
       GROUP BY COMP_CODE,MAIN_CODE,MAT_TYPE,MATCODE,NVL(ATTRIB_CODE1,0),NVL(ATTRIB_CODE2,0) 

     ) 
     --WHERE MATCODE='168847' 
     GROUP BY COMP_CODE,MAIN_CODE,UNIT_CODE,MAT_TYPE,MATCODE,ATTRIB_CODE1,ATTRIB_CODE2 
     ) D 

WHERE C.COMP_CODE   = D.COMP_CODE (+) 
AND  C.UNIT_CODE   = D.UNIT_CODE(+) 
AND  C.MATCODE   = D.MATCODE(+) 
--AND C.MATCODE='168847' 
AND  C.ATTRIB1   = D.ATTRIB_CODE1(+) 
AND  C.ATTRIB2   = D.ATTRIB_CODE2(+) 
AND C.COMP_CODE=:P_COMP_CODE 
AND C.UNIT_CODE=DECODE(:P_UNIT_CODE,'ALL',C.UNIT_CODE,:P_UNIT_CODE) 
GROUP BY 
C.COMP_CODE,C.MATCODE,C.ATTRIB1,C.ATTRIB2,C.MAT_NAME,C.SUP_PROD_CODE, 
C.SUP_CODE,C.BRAND_CODE,C.CAT_CODE,SGRPCODE,SUB_SGRPCODE,C.UNIT_CODE,GRN_DATE 
order by c.unit_code 

的參數是(:FR_DT,:TO_DT,:p_COMP_CODE,:FR_CAT,:TO_CAT,:p_SUP_CODE) '?'這就需要被替換同時在數據集中寫入查詢。但我不知道如何用查詢參數替換多個地方發生的相同參數。而如何處理DECODE和指標的影響之間。

回答

2

一種選擇是使用WITH條款爲您的參數分配給虛擬表。

WITH tmp_parms AS (
    SELECT ? as fr_dt, ? as to_dt, ? as p_comp_code, 
     ? as fr_cat, ? as to_cat, ? as p_sub_code 
    FROM dual 
) 
SELECT C.COMP_CODE,C.MATCODE, ... etc 
FROM tmp_parms tp, 
     (
     SELECT A.COMP_CODE,A.MATCODE, ... etc 

或者,如果你更喜歡另一種內嵌視圖:

SELECT C.COMP_CODE,C.MATCODE, ... etc 
FROM (
    SELECT ? as fr_dt, ? as to_dt, ? as p_comp_code, 
     ? as fr_cat, ? as to_cat, ? as p_sub_code 
    FROM dual 
) tp, 
     (
     SELECT A.COMP_CODE,A.MATCODE, ... etc 

然後從臨時PARMS表等效列引用替換現有的所有綁定變量,即改變這一點:

       b.grn_date<=:TO_DT) 

這樣:

       b.grn_date<=tp.to_dt) 
+0

修改後執行查詢時預覽結果給出此異常 java.sq.SQLException:ORA_00904:「TR」。「P_COMP_CODE」:無效標識符 輸出列中沒有列。怎麼了。 – 2011-05-20 05:16:00

+0

我使用了WITH子句 – 2011-05-20 05:17:49

+0

我使用** TP **作爲表別名,而不是** TR **;錯誤提示你將你的where子句改成了'tr.p_comp_code'而不是'tp.p_comp_code'? – 2011-05-20 06:23:33