SQL problem - Queries Quality and Percentage
Problem
Query
SELECT
query_name,
ROUND(AVG(rating / position), 2) AS quality,
ROUND(SUM(IF(rating < 3, 1, 0)) / COUNT(*) * 100, 2) AS poor_query_percentage
FROM
Queries
WHERE
query_name IS NOT NULL
GROUP BY
query_name
Approach: CASE WHEN instead of IF
select
query_name,
round(avg(cast(rating as decimal) / position), 2) as quality,
round(sum(case when rating < 3 then 1 else 0 end) * 100 / count(*), 2) as poor_query_percentage
from
queries
group by
query_name;