¡Buen día a todos!
¿Pueden ayudarme a corregir esta consulta en MySQL? Simplemente estoy generando una tabla pivote y quiero retornar con SUM la suma de cada fila, pero actualmente me arroja un valor incorrecto. Las tablas son las siguientes:
lab_registered_problems
+--------+------------+------------+----------+ | id_lab | id_problem | subproblem | weighing | +--------+------------+------------+----------+ | 123456 | 55 | 1 | 20 | | 123456 | 55 | 2 | 30 | | 123456 | 55 | 3 | 50 | +--------+------------+------------+----------+
lab_registered_users
+--------+---------+ | id_lab | id_user | +--------+---------+ | 123456 | User1 | | 123456 | User2 | | 123456 | User3 | +--------+---------+
lab_user_scores
+--------+------------+------------+---------+-------+ | id_lab | id_problem | subproblem | id_user | score | +--------+------------+------------+---------+-------+ | 123456 | 55 | 1 | User1 | 100 | | 123456 | 55 | 2 | User2 | 50 | +--------+------------+------------+---------+-------+
La consulta SQL es la siguiente:
SELECT lab_registered_users.id_user,
MAX(IF(lab_user_scores.id_problem = 55 AND lab_user_scores.subproblem = 1 AND lab_user_scores.id_user = lab_registered_users.id_user,lab_user_scores.score * lab_registered_problems.weighing / 100.0,0)) AS "55/1",
MAX(IF(lab_user_scores.id_problem = 55 AND lab_user_scores.subproblem = 2 AND lab_user_scores.id_user = lab_registered_users.id_user,lab_user_scores.score * lab_registered_problems.weighing / 100.0,0)) AS "55/2",
MAX(IF(lab_user_scores.id_problem = 55 AND lab_user_scores.subproblem = 3 AND lab_user_scores.id_user = lab_registered_users.id_user,lab_user_scores.score * lab_registered_problems.weighing / 100.0,0)) AS "55/3",
SUM(lab_user_scores.score * lab_registered_problems.weighing / 100.0) AS total_score
FROM lab_registered_users
INNER JOIN lab_user_scores ON lab_user_scores.id_lab = lab_registered_users.id_lab
INNER JOIN lab_registered_problems ON lab_registered_problems.id_lab = lab_registered_users.id_lab AND lab_registered_problems.id_problem = lab_user_scores.id_problem AND lab_registered_problems.subproblem = lab_user_scores.subproblem
WHERE lab_registered_users.id_lab = 123456
GROUP BY lab_registered_users.id_user
La cual me genera la siguiente tabla:
+---------+---------+---------+--------+-------------+ | id_user | 55/1 | 55/2 | 55/3 | total_score | +---------+---------+---------+--------+-------------+ | User1 | 20.0000 | 0.0000 | 0.0000 | 35.0000 | | User2 | 0.0000 | 15.0000 | 0.0000 | 35.0000 | | User3 | 0.0000 | 0.0000 | 0.0000 | 35.0000 | +--------+----------+---------+--------+-------------+
Sin embargo, lo que quiero conseguir es lo siguiente:
+---------+---------+---------+--------+-------------+ | id_user | 55/1 | 55/2 | 55/3 | total_score | +---------+---------+---------+--------+-------------+ | User1 | 20.0000 | 0.0000 | 0.0000 | 20.0000 | | User2 | 0.0000 | 15.0000 | 0.0000 | 15.0000 | | User3 | 0.0000 | 0.0000 | 0.0000 | 0.0000 | +--------+----------+---------+--------+-------------+
¡Muchas gracias por tu valioso tiempo!