SUM子类别合而为一

时间:2016-07-11 01:50:03

标签: sql sum teradata

我正在考虑为以下查询添加日期范围。任何帮助都会非常好,谢谢你。

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 :(得分: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);