79624667

Date: 2025-05-16 07:19:46
Score: 1
Natty:
Report link
https://stackoverflow.com/questions/79604979/condensing-a-query-into-a-single-better-formatted-query
 
Updated Query
==========
SELECT 

    students.DriverLicense,

    SUM(CASE WHEN students.QuizTitle LIKE 'THEORY%' THEN students.Earned ELSE 0 END) AS Theory,

    SUM(CASE WHEN students.QuizTitle LIKE 'HAZMAT%' THEN students.Earned ELSE 0 END) AS Hazmat,

    SUM(CASE WHEN students.QuizTitle LIKE 'PASS%' THEN students.Earned ELSE 0 END) AS Pass,

    SUM(CASE WHEN students.QuizTitle LIKE 'SCHOOL%' THEN students.Earned ELSE 0 END) AS Bus

FROM students

WHERE students.DriverLicense = 'D120001102'

GROUP BY students.DriverLicense;

This query will do the following
 
1.It sums Earned only for matching QuizTitle values using CASE.

2.All results are returned in one row, grouped by DriverLicense.

3.It avoids using multiple subqueries or UNION.
 
https://www.pqube.us/
Reasons:
  • Blacklisted phrase (1): stackoverflow
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: PQube Business Solutions