1

我是 Oracle 的新手,我想知道是否有人可以告诉我我的 GROUP BY 表达式有什么问题。我已经尝试删除 GROUP BY 中的几个列,但仍然没有任何效果。还有另一个查询与此共享一个 UNION,但我已将其删除以减少阅读。

 -- Selection Criteria: Date Range = 12/01/2011 to 12/31/2011; AO Transaction Fee = 4.95%

  SELECT 
    SUBSTR(Transaction_Date,1,10) Transaction_Date
, Item_Number
, REGEXP_REPLACE(SUBSTR(Item_Or_Adj_Description,1,50)
,'([^[:print:]])',' ') AS Item_Or_Adj_Desctription
, Customer_Type
, Document_ID
, Dealer_ID
, Sales_Type
, Item_Quantity
, Total_Fee
, State_Fee
, Transaction_Fee
, AO_Fee
, WDFW_Fee 

  FROM 
  ( 
    -- Sales Transactions 
   SELECT /*+ index(IT ITEM_X4) */ 
      TO_CHAR(IT.it_status_set_date - 2/24, 'MM/DD/YYYY') AS Transaction_Date,  -- Pacific Time
      TO_NUMBER(IT.ic_rcn) AS Item_Number, IT.it_descr AS Item_Or_Adj_Description, 
      DT.di_name AS Customer_Type, IT.it_docid AS Document_ID, IT.ag_id AS Dealer_ID, 
      CASE WHEN UPPER(IST.is_name) = 'ACTIVE' THEN 'SALE' ELSE IST.is_name END AS Sales_Type, 
      NVL(IT.it_quantity * CASE WHEN IT.is_id = 'AC' THEN 1 WHEN IT.is_id = 'DU' THEN 1 ELSE -1 END, 0) AS Item_Quantity,  -- Dups = 1
      NVL(IT.it_state_fee * CASE WHEN IT.is_id IN ('DC','SC') THEN -1.00 ELSE 1.00 END, 0.00) + 
    NVL(IT.it_other_fee * CASE WHEN IT.is_id IN ('DC','SC') THEN -1.00 ELSE 1.00 END, 0.00) AS Total_Fee, 
      NVL(IT.it_state_fee * CASE WHEN IT.is_id IN ('DC','SC') THEN -1.00 ELSE 1.00 END, 0.00) AS 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 Transaction_Fee, 
      CASE WHEN IT.it_other_fee IS NULL OR IT.it_other_fee = 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.95 / 100), 2) 
      END AS AO_Fee, 
      CASE WHEN IT.it_other_fee IS NULL OR IT.it_other_fee = 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.95 / 100)), 2) 
      END AS WDFW_Fee 
   FROM ITEM IT 
   JOIN DISCOUNT_TYPE DT ON DT.di_id = IT.di_id 
   JOIN ITEM_STATUS_TYPE IST ON IST.is_id = IT.is_id 
   WHERE IT.it_status_ind = 'A' -- Include active ITEM rows only. 
   AND (IT.is_id IN ('AC','DC','SC') OR (IT.is_id = 'DU' AND NVL(IT.it_state_fee, 0) != 0)) -- Exclude voids, exchanges, and false duplicates.
   AND IT.ic_rcn != '999' -- Exclude Dealer Fees. 
   AND IT.it_status_set_date BETWEEN TO_DATE('12/01/2011 00:00:00', 'MM/DD/YYYY HH24:MI:SS') + 2/24  -- Pacific Time
                 AND TO_DATE('12/31/2011 23:59:59', 'MM/DD/YYYY HH24:MI:SS') + 2/24 )


  GROUP BY TO_DATE(SUBSTR(Transaction_Date,1,10), 'MM/DD/YYYY')
           , Item_Number
           , Item_Or_Adj_Description
           ,Customer_Type
           ,Document_ID
           ,Dealer_ID
           ,Sales_Type
           ,Item_Quantity;
          -- ,Total_Fee
         --  ,State_Fee
          -- ,Transaction_Fee
        --  ,AO_Fee
        --  ,WDFW_Fee 
4

2 回答 2

2

您必须删除选择列表中的列,而不是group by子句中的列,或者您可能会在group by子句中添加其他列。选择列表中的所有列都必须在group by子句中提及,或者必须是聚合函数,如 min、max 或 count。

于 2012-04-04T16:51:04.823 回答
2

您似乎没有在查询中进行任何聚合(列表中的所有列都不SELECT是聚合函数,如COUNTor MAX)。鉴于此,您应该完全删除该GROUP BY子句。

如果像 phlogratos 建议的那样,您尝试使用 aGROUP BY来消除重复项,那么您做错了什么。最常见的问题是查询本身不应该生成重复的行,但是缺少某些连接条件或谓词,从而导致行重复。如果是这种情况,您最好解决根本问题并添加缺少的谓词或缺少的连接。如果你真的希望内联视图返回重复的行并且你想删除它们,你最好做一个SELECT DISTINCT <<list of columns>>with noGROUP BY子句。

于 2012-04-04T16:52:59.023 回答