Mysql SQL聚合分数
我有一张有City和ComplaintType的桌子 我正在尝试创建具有以下计算的规范化列: (伪)选择特定城市中特定类型的编号)/(特定城市中所有投诉的编号 我目前拥有以下SQL:Mysql SQL聚合分数,mysql,sql,Mysql,Sql,我有一张有City和ComplaintType的桌子 我正在尝试创建具有以下计算的规范化列: (伪)选择特定城市中特定类型的编号)/(特定城市中所有投诉的编号 我目前拥有以下SQL: SELECT City AS city_name, ComplaintType AS complaint_type, count(*) / (SELECT count(City) FROM data GROUP BY City) AS complaint_frac, count(*) AS cou
SELECT City AS city_name, ComplaintType AS complaint_type,
count(*) / (SELECT count(City) FROM data GROUP BY City) AS complaint_frac,
count(*) AS count_freq,
(SELECT count(City) FROM data GROUP BY City) AS count_city
FROM data
GROUP BY City, ComplaintType
ORDER BY complaint_frac DESC
这给了我下表:
城市中的投诉总数(count\u city
)不正确。但是,当我自己运行count\u city查询时,计数是正确的,并给出以下输出:
如何正确获取与城市投诉数量相关的city\u计数
,以便计算正确的分数
冷硬数字示例:
布朗克斯和热水=79690
布朗克斯(投诉总数)=579363
投诉分数=79690/579363=0.13754761695
关联主表中的
子查询
SELECT City AS city_name, ComplaintType AS complaint_type,
count(*) / (SELECT count(City) FROM data GROUP BY City) AS complaint_frac,
count(*) AS count_freq,
(SELECT count(d1.City) FROM data d1 WHERE d1.City = d2.City GROUP BY d1.City) AS count_city
FROM data d2
GROUP BY City, ComplaintType
ORDER BY complaint_frac DESC
关联主表中的
子查询
SELECT City AS city_name, ComplaintType AS complaint_type,
count(*) / (SELECT count(City) FROM data GROUP BY City) AS complaint_frac,
count(*) AS count_freq,
(SELECT count(d1.City) FROM data d1 WHERE d1.City = d2.City GROUP BY d1.City) AS count_city
FROM data d2
GROUP BY City, ComplaintType
ORDER BY complaint_frac DESC
这不需要子查询,至少在MySQL 8+中是这样;窗口函数可以完成以下工作:
SELECT City AS city_name, ComplaintType AS complaint_type,
count(*) / sum(count(*)) over (partition by city) as complaint_frac,
count(*) as count_freq,
sum(count(*)) over (partition by city) as count_city
FROM data
GROUP BY City, ComplaintType
ORDER BY complaint_frac DESC
这不需要子查询,至少在MySQL 8+中是这样;窗口函数可以完成以下工作:
SELECT City AS city_name, ComplaintType AS complaint_type,
count(*) / sum(count(*)) over (partition by city) as complaint_frac,
count(*) as count_freq,
sum(count(*)) over (partition by city) as count_city
FROM data
GROUP BY City, ComplaintType
ORDER BY complaint_frac DESC