简体   繁体   English

带案例语句的分组依据失败子句

[英]Group by with case statement fails group by clause

I have a table: 我有一张桌子:

PROCEDURE
---------
id  INT
SERVICE_TYPE  VARCHAR(20)
TIMEUNITS (INT)
UNITS (INT)

I am trying to run this sql code: 我正在尝试运行以下sql代码:

select SERVICE_TYPE, 
       case when TIMEUNITS > 1 THEN SUM(1) 
       WHEN UNITS > 1 THEN SUM(UNITS) 
       END
group by SERVICE_TYPE

My problem is that I"m told that TIMEUNITS is not grouped nor aggregated by... 我的问题是,我被告知TIMEUNITS没有按以下类别进行分组或汇总:

how do I repair this query? 如何修复此查询? thanks 谢谢

For conditional aggregation, the case expression is the argument to the aggregation function. 对于条件聚合, case表达式是聚合函数的参数。 For example, to get the number of rows where timeunits > 1 : 例如,要获取timeunits > 1的行数:

select SERVICE_TYPE,
       sum(case when timeunits > 1 then 1 else 0 end)
from t
group by SERVICE_TYPE;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM