尝试在Oracle中使用UNION

时间:2012-09-17 23:21:36

标签: oracle plsql

我有两个问题,我正在尝试UNION。两个查询分开工作,我很确定所有数据类型都匹配。我在第一个查询中得到错误“not a group by expression”。我还得到了第二个“Dealer_label无效的指令”。我意识到这是一个很长的问题,但如果有人看到了一些东西,那么我会很感激。感谢

SELECT Item_Category
        , 'CATEGORY: ' || Item_Category AS Item_Category_Header
        , Item_SubCategory
        , Item_Category || ' TOTALS: ' AS Item_Category_Total
              ,Item_RCN
              , (SELECT SUBSTR(TRIM(IC2.ic_short_descr),1,50) FROM ITEM_CATALOG IC2 WHERE TO_NUMBER(IC2.ic_rcn) = Item_RCN AND IC2.ls_year = License_Year AND ROWNUM = 1) AS Item_Description
              ,License_Year
              , Discount_Type
              , Status_Type
              ,Dealer_Fee 
              , Item_State_Fee
              , Item_Trans_Fee
              , Total_Licenses_Sold
              , Total_State_Fee
              , Total_Trans_Fee
              , AO_Trans_Fee
              , WDFW_Trans_Fee
        FROM (SELECT Item_Category
                     , Item_SubCategory
                     , Item_RCN
                     , License_Year
                     , Discount_Type
                     , Status_Type
                     , Dealer_Fee 
                     ,Item_State_Fee, Item_Trans_Fee
                     ,SUM(Total_Licenses_Sold) AS Total_Licenses_Sold
                     , SUM(Total_State_Fee) AS Total_State_Fee
                     , SUM(Total_Trans_Fee) AS Total_Trans_Fee
                     ,SUM(AO_Trans_Fee) AS AO_Trans_Fee
                     , SUM(WDFW_Trans_Fee) AS WDFW_Trans_Fee
              FROM (SELECT /*+ index(IT ITEM_X4) */ 
                          TRIM(LT.lt_descr) AS Item_Category
                          , TRIM(ITT.itt_name) AS Item_SubCategory
                          , TO_NUMBER(IT.ic_rcn) AS Item_RCN
                          , LS.ls_year AS License_Year
                          ,SUBSTR(TRIM(DT.di_descr),1,3) AS Discount_Type
                          , IT.is_id AS Status_Type
                          , 0.00  AS Dealer_Fee
                          ,CASE WHEN NVL(IT.it_state_fee, 0.00) = 0.00 THEN 0.00 ELSE IT.it_state_fee / NVL(IT.it_quantity, 1) END AS Item_State_Fee
                          ,CASE WHEN NVL(IT.it_other_fee, 0.00) = 0.00 THEN 0.00 ELSE IT.it_other_fee / NVL(IT.it_quantity, 1) END AS Item_Trans_Fee
                          ,NVL(IT.it_quantity * CASE WHEN IT.is_id IN ('DC','SC') THEN -1 ELSE 1 END, 0) AS Total_Licenses_Sold
                          ,NVL(IT.it_state_fee * CASE WHEN IT.is_id IN ('DC','SC') THEN -1.00 ELSE 1.00 END, 0.00) AS Total_State_Fee
                          ,NVL(IT.it_other_fee * CASE WHEN IT.is_id IN ('DC','SC') THEN -1.00 ELSE 1.00 END, 0.00) AS Total_Trans_Fee
                          ,CASE WHEN NVL(IT.it_other_fee, 0.00) = 0.00 OR NVL(4, 0.00) = 0.00 THEN 0.00 
                            ELSE ROUND(IT.it_state_fee * CASE WHEN IT.is_id IN ('DC','SC') THEN -1.00 ELSE 1.00 END * (4 / 100), 2) 
                           END AS AO_Trans_Fee
                          ,CASE WHEN NVL(IT.it_other_fee, 0.00) = 0.00 OR NVL(4, 0.00) = 0.00 THEN 0.00 
                            ELSE (IT.it_other_fee * CASE WHEN IT.is_id IN ('DC','SC') THEN -1.00 ELSE 1.00 END) - 
                              ROUND((IT.it_state_fee * CASE WHEN IT.is_id IN ('DC','SC') THEN -1.00 ELSE 1.00 END * (4 / 100)), 2) 
                           END AS WDFW_Trans_Fee
                    FROM ITEM IT
                    JOIN ITEM_CATALOG IC ON IC.ic_id = IT.ic_id
                    JOIN DISCOUNT_TYPE DT ON DT.di_id = IT.di_id
                    JOIN LICENSE_TYPE LT ON LT.lt_id = IT.lt_id
                    JOIN ITEM_TYPE ITT ON ITT.itt_id = IT.itt_id
                    JOIN LICENSE_SEASON LS ON LS.ls_id = IT.ls_id
                    JOIN AGENT AG ON AG.ag_id = IT.ag_id
                    JOIN AGENT_TYPE AGT ON AGT.at_id = AG.at_id
                    LEFT JOIN CORPORATION CP ON CP.cp_id = AG.cp_id
                    LEFT JOIN BUSINESS_TYPE BT ON BT.bt_id = AG.bt_id
                    WHERE IT.it_status_ind = 'A'
                      AND (IT.is_id IN ('AC','DC','SC','EX') OR (IT.is_id = 'DU' AND NVL(IT.it_state_fee, 0) != 0)) -- Exclude voids and false duplicates.
                      AND IT.ic_rcn != '999' -- Exclude Dealer Fees.
                      AND IT.it_status_set_date BETWEEN TO_DATE('04/01/2012' || ' 00:00:00', 'MM/DD/YYYY HH24:MI:SS') + 2/24
                                                    AND TO_DATE('04/07/2012' || ' 23:59:59', 'MM/DD/YYYY HH24:MI:SS') + 2/24 
                 /*     AND (:p_ls_year IS NULL OR LS.ls_year = :p_ls_year)
                      AND (:p_dealer_id IS NULL OR AG.AG_ID = :p_dealer_id)
                      AND (:p_discount_type IS NULL OR DT.di_id = :p_discount_type)
                      AND (:p_corp_id IS NULL OR CP.CP_ID = :p_corp_id)
                      AND (:p_lt_id IS NULL OR LT.lt_id = :p_lt_id) --Category
                      AND (:p_itt_id IS NULL OR ITT.itt_id = :p_itt_id) --SubCategory*/


                  ) ReportRollup
        GROUP BY Item_Category, Item_SubCategory, Item_RCN, License_Year, Discount_Type, Status_Type, Item_State_Fee, Item_Trans_Fee
        ORDER BY Item_Category, Item_SubCategory, Item_RCN, License_Year, Discount_Type, Status_Type, Item_State_Fee, Item_Trans_Fee
        ) ReportDetails  

  UNION

    SELECT     'DEALER FEES' AS Item_Category
           ,'CATEGORY: DEALER FEES' AS Item_Category_Header
           ,'DEALER FEES' AS Item_Subcategory
           ,'DEALER FEE TOTALS:' AS Item_Category_Total
           ,Report_Group AS Item_RCN
            ,Dealer_Label AS Item_Description
            ,License_Year
            ,'DF' AS Discount_Type 
            ,'DF' AS Status_Type 
            ,SUM(Dealer_Fee) AS Dealer_Fee
            ,0 AS Item_State_Fee 
            ,0 AS Item_Trans_Fee 
            ,0 AS Total_Licenses_Sold 
            ,0 AS Total_State_Fee 
            ,0 AS Total_Trans_Fee
            ,0 AS AO_Trans_Fee 
            ,0 AS WDFW_Trans_Fee 
      FROM (SELECT /*+ index(IT ITEM_X4) */ 
              CASE 
                WHEN AGT.at_id = 7 THEN 9996 -- Internet.
                WHEN AGT.at_id = 4 THEN 9997 -- Telephone.
                WHEN NVL(AG.bt_id,'O') = 'S' THEN 9998 -- State Agency.
                ELSE 9999 -- All others.
              END Report_Group
              ,CASE WHEN AGT.at_id = 7 THEN TO_CHAR(AG.ag_id) -- Internet.
                 WHEN AGT.at_id = 4 THEN TO_CHAR(AG.ag_id) -- Telephone.
                 WHEN NVL(AG.bt_id,'O') = 'S' THEN TRIM(AG.ag_name) || ' - ' || TO_CHAR(AG.ag_id)   -- State Agency.
                 ELSE 'ALL OTHERS'
               END Dealer_Label
              ,LS.ls_year License_Year
              ,NVL(IT.it_agent_fee * CASE 
                    WHEN IT.is_id IN ('DC','SC') 
                    THEN -1.00 
                    ELSE 1.00 
                   END, 0.00) Dealer_Fee
              FROM ITEM IT
              JOIN ITEM_CATALOG IC ON IC.ic_id = IT.ic_id
              JOIN DISCOUNT_TYPE DT ON DT.di_id = IT.di_id
              JOIN LICENSE_TYPE LT ON LT.lt_id = IT.lt_id
              JOIN ITEM_TYPE ITT ON ITT.itt_id = IT.itt_id
              JOIN LICENSE_SEASON LS ON LS.ls_id = IT.ls_id
              JOIN AGENT AG ON AG.ag_id = IT.ag_id
              JOIN AGENT_TYPE AGT ON AGT.at_id = AG.at_id
              LEFT JOIN CORPORATION CP ON CP.cp_id = AG.cp_id
              LEFT JOIN BUSINESS_TYPE BT ON BT.bt_id = AG.bt_id
              WHERE IT.it_status_ind = 'A'
                AND (IT.is_id IN ('AC','DC','SC','EX') OR (IT.is_id = 'DU' AND NVL(IT.it_state_fee, 0) != 0)) -- Exclude voids and false duplicates.
                AND IT.ic_rcn = '999' -- Exclude Dealer Fees.
                AND IT.it_status_set_date BETWEEN TO_DATE('04/01/2012'|| ' 00:00:00', 'MM/DD/YYYY HH24:MI:SS') + 2/24
                                              AND TO_DATE('04/07/2012' || ' 23:59:59', 'MM/DD/YYYY HH24:MI:SS') + 2/24 
      /*    AND (:p_ls_year IS NULL OR LS.ls_year = :p_ls_year)
                AND (:p_dealer_id IS NULL OR AG.AG_ID = :p_dealer_id)
                AND (:p_discount_type IS NULL OR DT.di_id = :p_discount_type)
                AND (:p_corp_id IS NULL OR CP.CP_ID = :p_corp_id)
                AND (:p_lt_id IS NULL OR LT.lt_id = :p_lt_id) --Category
                AND (:p_itt_id IS NULL OR ITT.itt_id = :p_itt_id) --SubCategory*/ 

           ORDER BY Report_Group     
              ) DealerFees
      GROUP BY Report_Group
              ,Dealer_Label
              ,License_Year
      ORDER BY Dealer_Label
              ,License_Year
 ;

1 个答案:

答案 0 :(得分:0)

列表行:

           ,Dealer_Label AS Item_Description

将子查询的Dealer_Label重命名为Item_Description,以便:

      GROUP BY Report_Group
              ,Dealer_Label
              ,License_Year
      ORDER BY Dealer_Label
              ,License_Year

应该是

      GROUP BY Report_Group
              ,Item_Description
              ,License_Year
      ORDER BY Item_Description
              ,License_Year

或从选择列表中删除新标签。