我用的是this MySQL table。
我试图找出每种语言在30天内的百分比。
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'
我明白了我在尝试第二种方法时所犯的错误。
2条答案
按热度按时间nzrxty8p1#
我想你需要窗口函数:
这将按语言聚合表,然后计算计数;表达式
sum(count(*)) over()
计算所有计数的总和-您可以将其用作除法的denumerator。注:
between
来过滤日期(如果日期有时间部分,通常效果会更好)e0bqpujr2#