1

I have this SQL Query, where i have a few case statements and count in order to pivot my "clase x" rows and it returns something like this.

| Nombre Completo | Clase 1 | Clase 2|
--------------------------------------
| Example name   |   3      |    5   |
--------------------------------------

And what I need, its to replace the number, if the number its > 0 then replace it with a yes, else replace it with NO. How can I do that? Mysql Query:

SELECT  DISTINCT concat_ws(' ',U.firstname, U.lastname) as  Nombre_Completo,
COUNT(CASE WHEN b.name="Clase 1" THEN b.name END) Clase1,
COUNT(CASE WHEN b.name="Clase 2" THEN b.name END) Clase2,
COUNT(CASE WHEN b.name="Clase 3" THEN b.name END) Clase3,
COUNT(CASE WHEN b.name="Clase 4" THEN b.name END) Clase4,
COUNT(CASE WHEN b.name="Clase 5" THEN b.name END) Clase5,
COUNT(CASE WHEN b.name="Clase 6" THEN b.name END) Clase6,
COUNT(CASE WHEN b.name="Clase 7" THEN b.name END) Clase7,
COUNT(CASE WHEN b.name="Clase 8" THEN b.name END) Clase8,
COUNT(CASE WHEN b.name="Clase 9" THEN b.name END) Clase9,
COUNT(CASE WHEN b.name="Clase 10" THEN b.name END) Clase10,
COUNT(CASE WHEN b.name="Clase 11" THEN b.name END) Clase11,
COUNT(CASE WHEN b.name="Clase 12" THEN b.name END) Clase12
FROM mdl_logstore_standard_log as L
LEFT JOIN mdl_course as C ON L.courseid = C.id 
left JOIN mdl_user as U on L.userid = U.ID
JOIN mdl_bigbluebuttonbn AS b ON L.objectid = b.id
JOIN mdl_role_assignments AS ra ON L.userid = ra.userid
JOIN mdl_role AS r ON r.id = ra.roleid
WHERE (C.shortname LIKE '%CSG012020%') AND (ra.roleid=5)  AND (ra.roleid NOT IN (3,4))
GROUP BY Nombre_Completo;

2 Answers 2

1

Every:

COUNT(CASE WHEN b.name="Clase X" THEN b.name END)

in your code can be written as:

SUM(b.name='Clase X')

because MySql evaluates boolean expressions like b.name='Clase 1' as 1 (true) or 0 (false).

Also, any integer value can be interpreted as false if it is 0 or true if it is different than 0.

So you can write your query like this:

SELECT concat_ws(' ', U.firstname, U.lastname) Nombre_Completo,
       CASE WHEN SUM(b.name='Clase 1') THEN 'yes' ELSE 'no' END Clase1,
       CASE WHEN SUM(b.name='Clase 2') THEN 'yes' ELSE 'no' END Clase2,
       CASE WHEN SUM(b.name='Clase 3') THEN 'yes' ELSE 'no' END Clase3,
       CASE WHEN SUM(b.name='Clase 4') THEN 'yes' ELSE 'no' END Clase4,
       CASE WHEN SUM(b.name='Clase 5') THEN 'yes' ELSE 'no' END Clase5,
       CASE WHEN SUM(b.name='Clase 6') THEN 'yes' ELSE 'no' END Clase6,
       CASE WHEN SUM(b.name='Clase 7') THEN 'yes' ELSE 'no' END Clase7,
       CASE WHEN SUM(b.name='Clase 8') THEN 'yes' ELSE 'no' END Clase8,
       CASE WHEN SUM(b.name='Clase 9') THEN 'yes' ELSE 'no' END Clase9,
       CASE WHEN SUM(b.name='Clase 10') THEN 'yes' ELSE 'no' END Clase10,
       CASE WHEN SUM(b.name='Clase 11') THEN 'yes' ELSE 'no' END Clase11,
       CASE WHEN SUM(b.name='Clase 12') THEN 'yes' ELSE 'no' END Clase12
FROM mdl_logstore_standard_log as L
LEFT JOIN mdl_course as C ON L.courseid = C.id 
left JOIN mdl_user as U on L.userid = U.ID
JOIN mdl_bigbluebuttonbn AS b ON L.objectid = b.id
JOIN mdl_role_assignments AS ra ON L.userid = ra.userid
JOIN mdl_role AS r ON r.id = ra.roleid
WHERE (C.shortname LIKE '%CSG012020%') AND (ra.roleid=5)  AND (ra.roleid NOT IN (3,4))
GROUP BY Nombre_Completo;

I removed DISTINCT because it is not needed since you GROUP BY Nombre_Completo.

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

Comments

1

Use IF() to test the result of COUNT()

SELECT  DISTINCT concat_ws(' ',U.firstname, U.lastname) as  Nombre_Completo,
IF(COUNT(CASE WHEN b.name="Clase 1" THEN b.name END) > 0, 'Yes', 'No') Clase1,
IF(COUNT(CASE WHEN b.name="Clase 2" THEN b.name END) > 0, 'Yes', 'No') Clase2,
IF(COUNT(CASE WHEN b.name="Clase 3" THEN b.name END) > 0, 'Yes', 'No') Clase3,
IF(COUNT(CASE WHEN b.name="Clase 4" THEN b.name END) > 0, 'Yes', 'No') Clase4,
IF(COUNT(CASE WHEN b.name="Clase 5" THEN b.name END) > 0, 'Yes', 'No') Clase5,
IF(COUNT(CASE WHEN b.name="Clase 6" THEN b.name END) > 0, 'Yes', 'No') Clase6,
IF(COUNT(CASE WHEN b.name="Clase 7" THEN b.name END) > 0, 'Yes', 'No') Clase7,
IF(COUNT(CASE WHEN b.name="Clase 8" THEN b.name END) > 0, 'Yes', 'No') Clase8,
IF(COUNT(CASE WHEN b.name="Clase 9" THEN b.name END) > 0, 'Yes', 'No') Clase9,
IF(COUNT(CASE WHEN b.name="Clase 10" THEN b.name END) > 0, 'Yes', 'No') Clase10,
IF(COUNT(CASE WHEN b.name="Clase 11" THEN b.name END) > 0, 'Yes', 'No') Clase11,
IF(COUNT(CASE WHEN b.name="Clase 12" THEN b.name END) > 0, 'Yes', 'No') Clase12
FROM mdl_logstore_standard_log as L
LEFT JOIN mdl_course as C ON L.courseid = C.id 
left JOIN mdl_user as U on L.userid = U.ID
JOIN mdl_bigbluebuttonbn AS b ON L.objectid = b.id
JOIN mdl_role_assignments AS ra ON L.userid = ra.userid
JOIN mdl_role AS r ON r.id = ra.roleid
WHERE (C.shortname LIKE '%CSG012020%') AND (ra.roleid=5)  AND (ra.roleid NOT IN (3,4))
GROUP BY Nombre_Completo;

Comments

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.