Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用SQL获取每天分类变量的百分比?_Sql_Sql Server - Fatal编程技术网

如何使用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;