使用CASE WHEN

时间:2017-06-07 01:05:13

标签: sql teradata

我有下表:

Email | value | group
 a    |   1   |   x
 a    |   2   |   y
 a    |   3   |   y
 b    |   3   |   z

我之前编写的代码用于通过每组中的不同电子邮件来提取值的总和,以获得此结果:

Email | value | group
 a    |   1   |   x
 a    |   5   |   y
 b    |   3   |   z

代码看起来像这样:

 SELECT distinct email,
        group,
        sum (value)
  from t
 group by email, group

现在,我希望使用CASE对各组进行求和,以获得此输出:

 Email | value | super_group
 a     |   6    |   x_y
 b     |   3    |   z

但是这段代码不能工作:

 SELECT distinct email,
        CASE when group in (x,y) 
             then 'x_y'
             else 'z' 
        end as super_group,
        sum (value)
   from t
  group by email, super_group

2 个答案:

答案 0 :(得分:1)

您可以使用 ARRAY_AGG

另外,如果您使用的是GROUP BY,则不需要DISTINCT。试试这个:

首先,你必须像这样定义一个ARRAY类型:

-- note the varchar with size 20 is a sample you should pick yours
-- and the size of the array is also an example
CREATE TYPE group_array AS VARCHAR(20) ARRAY[100];

然后,您可以使查询返回数组类型聚合的数组类型。

SELECT email,
       sum (value) as value,
       ARRAY_AGG(group, NEW group_array()) as super_group
 FROM t
GROUP BY email

那应该给你结果:

 email   | value  | super_group
   a     |   6    |   ( x, y )
   b     |   3    |   ( z )

参考ARRAY_AGG

答案 1 :(得分:1)

如果您使用的是较新版本的Teradata,可以使用XMLAGG()执行此操作:

SELECT 
    email,
    sum(value),
    trim(trailing '_' FROM (XMLAGG(group || '_' ORDER BY group) (VARCHAR(50))))
FROM table
GROUP BY 1