2016-07-11 21 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

好的谢谢。道歉,我是董事会新成员。 –

相关问题