2016-07-11 24 views
0

我正在查看添加日期範圍到下面的查詢。任何幫助都會很棒,謝謝你。SUM子類別合併爲一個

select (case when col2 in ('Commercial', 'Non-commercial') then 'Commercial' 
      when col2 in ('OEM', 'CA-OEM') then 'OEM' 
      else 'col2' 
     end), 
     sum(col4) as col4 
from t 
group by (case when col2 in ('Commercial', 'Non-commercial') then 'Commercial' 
       when col2 in ('OEM', 'CA-OEM') then 'OEM' 
       else 'col2' 
      end); 

日期以SQL

SELECT Customer_type,TRX_DATE (FORMAT 'YYYY-MM') (CHAR(7)) AS BATCH_DATE_MONTH 
WHERE CAST(TRX_DATE AS DATE FORMAT 'mm/dd/yyyy') >= '02-01-2016' 
Group BY 1,2 
ORDER BY 1 

    Customer_Type BATCH_DATE_MONTH Number_Invoices Billed_Amount 
1 Commercial-Non-OEM 2016-02 48 382458.92 
2 Commercial- Non-OEM 2016-05 77 7646263.67 
5 Non-OEM 2016-05 81 11841927.23 
6 Non-OEM 2016-02 25 1630832.81 
9 OEM 2016-04 68 280264011.01 
10 OEM 2016-03 74 277606302.23 
13 CA-OEM 2016-03 23 947248.13 
14 CA-OEM 2016-05 17 420363.61 
17 US-OEM 2016-04 35 4627887.87 
18 US-OEM 2016-03 40 6450226.49 
19 US-OEM 2016-02 21 3267732.17 
+1

請標記與您正在使用的數據庫你的問題。 –

回答

0

如下圖所示,您可以使用聚合查詢與case

select (case when col2 in ('Commercial', 'Non-commercial') then 'Commercial' 
      when col2 in ('OEM', 'CA-OEM') then 'OEM' 
      else 'col2' 
     end), 
     sum(col4) as col4 
from t 
group by (case when col2 in ('Commercial', 'Non-commercial') then 'Commercial' 
       when col2 in ('OEM', 'CA-OEM') then 'OEM' 
       else 'col2' 
      end); 
+0

有沒有辦法選擇日期範圍?例如SELECT TRX_DATE(FORMAT'YYYY-MM')(CHAR(7))AS BATCH_DATE_MONTH WHERE CAST(TRX_DATE AS DATE FORMAT'mm/dd/yyyy')> = '02 -01-2016' –

+0

@WasimGreaves。 。 。如果您還有其他問題,應該將其作爲問題提出,而不是發表評論。您應該包含樣本數據,期望的結果,您擁有的任何查詢,以及正在使用您正在使用的數據庫標記問題。 –

+0

好的謝謝。道歉,我是董事會新成員。 –

相關問題