我在执行以下查询时遇到“无效使用组功能”错误。
Select id, faultdistribution, faulttype, faultseverity,
SUM(IF (faultdistribution='crs', SUM(IF(faultdistribution='crs',1,0))*8, 0)+
IF (faultdistribution='configuration', SUM(IF(faultdistribution='configuration',1,0))* 6, 0)+
IF (faulttype='business' AND faultseverity='fatal', SUM(IF(faulttype='business' AND faultseverity='fatal',1,0))* 4, 0)+
IF (faulttype='business' AND faultseverity='major', SUM(IF(faulttype='business' AND faultseverity='major',1,0))* 2, 0)+
IF (faulttype='business' AND faultseverity='moderate', SUM(IF(faulttype='business' AND faultseverity='moderate',1,0))* 5, 0)+
IF (faulttype='business' AND faultseverity='minor', SUM(IF(faulttype='business' AND faultseverity='minor',1,0))* 3, 0)+
IF (faulttype='look & feel' AND faultseverity='fatal', SUM(IF(faulttype='look & feel' AND faultseverity='fatal',1,0))* 2, 0)+
IF (faulttype='look & feel' AND faultseverity='major', SUM(IF(faulttype='look & feel' AND faultseverity='major',1,0))* 1, 0))
FROM tbl_fault WHERE product='DAS' AND faultdistribution='missed'
我做错了什么?请帮助!
答案 0 :(得分:4)
SUM(),COUNT(),AVG(),MIN(),MAX()等是需要您指定GROUP BY的聚合函数,除非您在SELECT中的每一列上使用它们。列表。
在您的情况下,查询应该通过在底部添加以下内容来工作:
GROUP BY id, faultdistribution, faulttype, faultseverity
...但是根据你所拥有的许多嵌套的IF,我不完全确定这会给你你正在寻找的输出。
答案 1 :(得分:1)
尝试这个缩短的查询
Select id, faultdistribution, faulttype, faultseverity,
IF (faultdistribution='crs', 1,0)*8 +
IF (faultdistribution='configuration', 1,0)* 6 +
IF (faulttype='business' AND faultseverity='fatal', 1,0)* 4 +
IF (faulttype='business' AND faultseverity='major', 1,0)* 2 +
IF (faulttype='business' AND faultseverity='moderate', 1,0)* 5 +
IF (faulttype='business' AND faultseverity='minor', 1,0)* 3 +
IF (faulttype='look & feel' AND faultseverity='fatal', 1,0)* 2 +
IF (faulttype='look & feel' AND faultseverity='major', 1,0)* 1 as mysum
FROM tbl_fault WHERE product='DAS' AND faultdistribution='missed'
group by id , faultdistribution ,faulttype ,faultseverity