2

I am having "Invalid use of group function" error while executing following query.

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'

Where i am doing it wrong? Kindly help!

1
  • Is id the unique key for that table? If so how do you want to group things up over rows? Commented Oct 7, 2013 at 10:50

2 Answers 2

4

SUM(), COUNT(), AVG(), MIN(), MAX(), etc. are aggregate functions that requires you to specify a GROUP BY, unless you're using them on every column in your SELECT-list.

In your case, the query should work by adding the following at the bottom:

GROUP BY id, faultdistribution, faulttype, faultseverity

...but judging from the many nested IF's you have, I'm not entirely sure that this would give you the output you're looking for.

Sign up to request clarification or add additional context in comments.

Comments

1

try this shortened query

 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 

5 Comments

@OmerZia try this shortened query
that's a great help! :)
Here is the schema : sqlfiddle.com/#!2/d2aac/19 While executing this query in PHP i am having 46 as a result. The result should be 6
i see just entries in your demo , can you put the whole data , so i can look at it ?
i mean where is 46 results , fill them by your data, in your example there is only 2 entries, so i dont know if it will get 6 results.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.