如何计算MySQL中的总百分比
我有以下问题:如何计算MySQL中的总百分比,mysql,Mysql,我有以下问题: select count(*) as headct , case when year(DOB) <= 1945 then 'Traditionalist' when year(DOB) >= 1946 and year(DOB) < 1965 then 'Baby Boomer' when year(DOB) >= 1965 and year(DOB) < 1981 then 'Gen X' when year(DOB) >= 1981 the
select count(*) as headct
, case
when year(DOB) <= 1945 then 'Traditionalist'
when year(DOB) >= 1946 and year(DOB) < 1965 then 'Baby Boomer'
when year(DOB) >= 1965 and year(DOB) < 1981 then 'Gen X'
when year(DOB) >= 1981 then 'Gen Y'
end as generation
from ret_active
group by Generation with rollup;
如何添加另一列,显示每个类别占总数的百分比?换句话说,我如何才能得到以下结果:
headct generation % of Total
-------------------------------------
1820 Baby Boomer 37%
2208 Gen Xm 45%
883 Gen Y 18%
17 Traditionalist 0%
4928 null 100%
谢谢
select count(*) as headct
, case
when year(DOB) <= 1945 then 'Traditionalist'
when year(DOB) >= 1946 and year(DOB) < 1965 then 'Baby Boomer'
when year(DOB) >= 1965 and year(DOB) < 1981 then 'Gen X'
when year(DOB) >= 1981 then 'Gen Y'
end as generation
, concat(round(count(*)/
(select count(*) from ret_active)*100,0),'%') as '% of Total'
from ret_active
group by Generation with rollup
select count(*) as headct
, case
when year(DOB) <= 1945 then 'Traditionalist'
when year(DOB) >= 1946 and year(DOB) < 1965 then 'Baby Boomer'
when year(DOB) >= 1965 and year(DOB) < 1981 then 'Gen X'
when year(DOB) >= 1981 then 'Gen Y'
end as generation
, concat(round(count(*)/
(select count(*) from ret_active)*100,0),'%') as '% of Total'
from ret_active
group by Generation with rollup