2

执行以下查询时出现“组函数使用无效”错误。

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'

我在哪里做错了?请帮忙!

4

2 回答 2

4

SUM()、COUNT()、AVG()、MIN()、MAX() 等是需要您指定 GROUP BY 的聚合函数,除非您在 SELECT 列表中的每一列上使用它们。

在您的情况下,查询应该通过在底部添加以下内容来工作:

GROUP BY id, faultdistribution, faulttype, faultseverity

...但是从您拥有的许多嵌套 IF 来看,我不完全确定这会给您提供您正在寻找的输出。

于 2013-10-07T10:42:45.600 回答
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 
于 2013-10-07T11:10:22.533 回答