如何使用SQL获取每天分类变量的百分比?
我一直被困在这一点上,但我的最终目标是获得总体数据的负百分比、正百分比和中性百分比,并按日期以及类别分组。多谢各位如何使用SQL获取每天分类变量的百分比?,sql,sql-server,Sql,Sql Server,我一直被困在这一点上,但我的最终目标是获得总体数据的负百分比、正百分比和中性百分比,并按日期以及类别分组。多谢各位 只需使用窗口功能: select mlsentimentzone, (count(*) * 1.0 / sum(count(*)) over ()) as ratio from t group by mlsentimentzone; 或者,如果希望按日期进行此操作,请使用条件聚合: select date, avg(case when mlsentim
只需使用窗口功能:
select mlsentimentzone,
(count(*) * 1.0 / sum(count(*)) over ()) as ratio
from t
group by mlsentimentzone;
或者,如果希望按日期进行此操作,请使用条件聚合:
select date,
avg(case when mlsentimentzone = 'negative' then 1.0 else 0.0 end) as negative,
avg(case when mlsentimentzone = 'neutral' then 1.0 else 0.0 end) as neutral,
avg(case when mlsentimentzone = 'positive' then 1.0 else 0.0 end) as positive
from t
group by date
order by date;