简体   繁体   中英

MYSQL SUM IF return multiple rows

Is there any option if categori.categori_type != 'comment' return comment null and SUM poor,fair,good,vgood,vgood,excellent,yes,no column otherwise return this columns 0. I have more than 1 comment but it s return only 1 comment.

 SELECT 
 categori.s_categori_id,categori.categori_name_en,categori.categori_name_ar,
 categori.categori_type,question.survey_id,question.question_en,
 question.question_ar,
 IF(categori.categori_type != 'comment',SUM(result.poor),0) AS poor,
 IF(categori.categori_type != 'comment',SUM(result.fair),0) AS fair,
 IF(categori.categori_type != 'comment',SUM(result.good),0) AS good,
 IF(categori.categori_type != 'comment',SUM(result.vgood),0) AS vgood,
 IF(categori.categori_type != 'comment',SUM(result.excellent),0) AS 
 excellent,
 IF(categori.categori_type != 'comment',SUM(result.yes),0) AS yes,
 IF(categori.categori_type != 'comment',SUM(result.no),0) As no,
 result.comment 
 FROM survey_categori AS categori 
 INNER JOIN survey_questions AS question
 ON categori.s_categori_id = question.s_categori_id 
 INNER JOIN survey_result AS result 
 ON result.s_question_id = question.survey_id 
 WHERE categori.survey_type = 'class'
 GROUP BY question.survey_id

use case when and group by clause properly

SELECT 
     categori.s_categori_id,categori.categori_name_en,categori.categori_name_ar,
     categori.categori_type,question.survey_id,question.question_en,
     question.question_ar,
    sum(case when categori.categori_type != 'comment' then result.poor else 0 end) as poor,
    sum(case when categori.categori_type != 'comment' then result.fair else 0 end) as fair,
    sum(case when categori.categori_type != 'comment' then result.good else 0 end) as good,
     sum(case when categori.categori_type != 'comment' then result.vgood else 0 end) as vgood,
     sum(case when categori.categori_type != 'comment' then result.excellent else 0 end) as excellent,
     sum(case when categori.categori_type != 'comment' then result.yes else 0 end) as yes,
     sum(case when categori.categori_type != 'comment' then result.no else 0 end) as no,

     case when categori.categori_type = 'comment' then result.comment   end as rcomment
     FROM survey_categori AS categori 
     INNER JOIN survey_questions AS question
     ON categori.s_categori_id = question.s_categori_id 
     INNER JOIN survey_result AS result 
     ON result.s_question_id = question.survey_id 
     WHERE categori.survey_type = 'class'
     GROUP BY categori.s_categori_id,categori.categori_name_en,categori.categori_name_ar,
     categori.categori_type,question.survey_id,question.question_en,
     question.question_ar,rcomment

You can use the SQL CASE STATEMENT In this case

The CASE statement is SQL's way of handling if/then logic

Every CASE statement must end with the END statement where the 'ELSE' statement is optional

In your case :

If Condition_1 then Action_1 should Happen Else Action_2 should happen End

This is what exactly CASE STATEMENT does

When there is Single Condition then we can write as below

Select CASE
       WHEN condition_1 THEN Action_1 Else Action_2 
       END AS <ALIAS_COLUMN_NAME>

If there are multiple conditions to be satisfied then we can write

Select CASE
       WHEN condition_1 THEN Action_1 Else Action_2  
       WHEN condition_2 THEN Action_3 Else Action_2   
       WHEN condition_3 THEN Action_4 Else Action_2
       END AS <ALIAS_COLUMN_NAME>

If we need to apply different aggregation logics on the case statement for every iteration of one by one record at a time seperated by comma(,) we can write it as

Select FUNCTION_1(CASE
       WHEN condition_1 THEN Action_1 Else Action_2  
       END) AS <ALIAS_COLUMN_NAME>,
       FUNCTION_1(CASE
       WHEN condition_2 THEN Action_1 Else Action_2  
       END) AS <ALIAS_COLUMN_NAME>,
       FUNCTION_1(CASE
       WHEN condition_3 THEN Action_1 Else Action_2  
       END) AS <ALIAS_COLUMN_NAME>
GROUP BY column1_in_condition_1

Since we are applying the aggregation logic we need to ensure the column is in group by clause

Hope this will help a bit while applying next time :)

Try below query: with case when

    SELECT 
     categori.s_categori_id,categori.categori_name_en,categori.categori_name_ar,
     categori.categori_type,question.survey_id,question.question_en,
     question.question_ar,
    sum(case when categori.categori_type != 'comment' then result.poor else 0 end) as poor,
    sum(case when categori.categori_type != 'comment' then result.fair else 0 end) as fair,
    sum(case when categori.categori_type != 'comment' then result.good else 0 end) as good,
     sum(case when categori.categori_type != 'comment' then result.vgood else 0 end) as vgood,
     sum(case when categori.categori_type != 'comment' then result.excellent else 0 end) as excellent,
     sum(case when categori.categori_type != 'comment' then result.yes else 0 end) as yes,
     sum(case when categori.categori_type != 'comment' then result.no else 0 end) as no,

     result.comment 
     FROM survey_categori AS categori 
     INNER JOIN survey_questions AS question
     ON categori.s_categori_id = question.s_categori_id 
     INNER JOIN survey_result AS result 
     ON result.s_question_id = question.survey_id 
     WHERE categori.survey_type = 'class'
     GROUP BY categori.s_categori_id,categori.categori_name_en,categori.categori_name_ar,
 categori.categori_type,question.survey_id,question.question_en,
 question.question_ar,result.comment

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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