I used this MySQL table.
I am trying to find the percentage share of each language over the course of 30 days from it.
select language as languages, round(100*count(*)/total, 2)
from job_data
cross join (select count(*) as total from job_data) sub
group by language;
The error is highlighted in the ‘Output’ tab.
WITH CTE AS (
SELECT Language, COUNT(job_id) AS num_jobs
FROM job_data
WHERE event IN("transfer","decision")
AND ds >= "2020-11-01" AND ds <= "2020-11-30"
GROUP BY language),
total AS (
SELECT COUNT(job_id) AS total_jobs
FROM job_data
WHERE event IN("transfer","decision")
AND ds >= "2020-11-01" AND ds <= "2020-11-30"
GROUP BY language)
SELECT language, ROUND(100.0*num_jobs/total_jobs, 2) AS perc_jobs
FROM CTE
ORDER BY perc_job DESC;
Error Code: 1054. Unknown column 'total_jobs' in 'field list'
I understood the error I got while I tried the second method.
2
Answers
I think you want window functions:
This aggregates your table by language, then computes the counts; expression
sum(count(*)) over()
computes the total of all counts – you can use it as a denumerator of the division.Notes:
between
(that typically works better if your dates have a time portion)