![](/img/trans.png)
[英]Calculating the percentage difference in values between two columns in SQL returning 0
[英]calculating percentage of two columns in SQL - using BigQuery
我正在尝试计算犯罪状态(clearance_status)占每个类别总犯罪(primary_type)的百分比,但我无法正确输出。
这是我的初始查询:
SELECT primary_type,
clearance_status,
round(count(clearance_status)/(SELECT count(primary_type) from `bigquery-publicdata.austin_crime.crime`)*100,2) as percentage
FROM `bigquery-public-data.austin_crime.crime`
WHERE primary_type = "Theft: BOV"
GROUP BY primary_type, clearance_status
result:
Row primary_type clearance_status percentage
1 Theft: BOV. Not cleared 8.59
2 Theft: BOV. null. 0.0
3 Theft: BOV Cleared by Arrest. 0.21
4 Theft: BOV Cleared by Exception 0.03
但是查看 COUNT(primary_type) 和 COUNT(clerance_status) 的结果
SELECT primary_type, count(primary_type) as count
FROM `bigquery-public-data.austin_crime.crime`
WHERE primary_type = "Theft: BOV"
GROUP BY primary_type
result:
Row primary_type count
1 Theft: BOV. 10545
SELECT clearance_status, count(clearance_status) as count_1
FROM `bigquery-public-data.austin_crime.crime`
WHERE primary_type = "Theft: BOV"
GROUP BY clearance_status
result:
Row clearance_status count_1
1 Not cleared. 10028
2 null 0
3 Cleared by Arrest. 242
4 Cleared by Exception 30
手动,我应该得到 95% 的“未清除”状态,但我的初始查询只产生 8.59。 使用 over() 给出相同的输出。
SELECT
primary_type,
clearance_status,
COUNT(*) AS clearance_status_count,
(SUM(COUNT(*)) OVER(PARTITION BY primary_type)) AS total,
COUNT(*) / (SUM(COUNT(*)) OVER(PARTITION BY primary_type)) AS ratio
FROM
`bigquery-public-data.austin_crime.crime`
GROUP BY
primary_type,
clearance_status
ORDER BY
primary_type,
clearance_status;
此查询适用于不同的primary_types
。 我包括计数和总数以表明该比率是正确的。
类似的问题在这里: 使用 GROUP BY 计算组的百分比
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.