将两个select的计数除以子查询得到一个百分比

nx7onnlm  于 2021-06-24  发布在  Mysql
关注(0)|答案(1)|浏览(253)

我已经编写了这个查询,但是它指出每个表都需要有自己的别名。我做错什么了?据我所知,我给这些表加了别名。

SELECT (a.total_complete_days / b.total_days) * 100 AS "% of complete days"
FROM
(SELECT COUNT(*) AS total_complete_days
FROM (
    SELECT email, job_date, SUM(job_minutes)
    FROM jobs
    GROUP BY email, job_date
    HAVING SUM(job_minutes) BETWEEN 200 and 1000
) a,
(SELECT COUNT(*) AS total_days
FROM (
    SELECT email, job_date, SUM(job_minutes)
    FROM jobs
    GROUP BY email, job_date
) b)
92vpleto

92vpleto1#

我想你可以把你的问题简化为

SELECT 
    (COUNT(CASE WHEN t.job_minutes_sum BETWEEN 200 and 1000 THEN 1 ELSE NULL END) / COUNT(*) 
    ) * 100 AS "% of complete days"
FROM (
    SELECT email, job_date, SUM(job_minutes) job_minutes_sum
    FROM jobs
    GROUP BY email, job_date
) t

相关问题