gpt4 book ai didi

python - 从Python sql查询中的总和计算百分比/分数

转载 作者:行者123 更新时间:2023-11-29 10:33:55 25 4
gpt4 key购买 nike

我有一个查询,可以获取某个城市的投诉数量。

query = '''
select ComplaintType as complaint_type, City as city_name,
count(ComplaintType) complaint_count
from data
where city in ({})
group by city_name, complaint_type
order by city_name
'''.format(strs_to_args(TOP_CITIES))

投诉类型 City_name .Complain_

现在,我想创建一个列来计算城市中发生的类型 t 的投诉。类似于 count(ComplaintType)/sum(count(ComplaintType) in city)

完成此任务的最佳语法是什么?

query = '''
select ComplaintType as complaint_type, City as city_name,
count(ComplaintType)/sum(count(ComplaintType) as complaint_freq

最佳答案

嗯,一种方法是在子查询中进行汇总并将结果连接到:

query = '''
select d.ComplaintType as complaint_type, d.City as city_name,
count(*) as complaint_count,
count(*) * 1.0 / max(cc.cnt) as ratio
from data d cross join
(select d.city, count(*) as cnt
from data d
group by d.city
) cc
on d.city = cc.city
where d.city in ({})
group by d.city, d.complaint_type
order by d.city
'''.format(strs_to_args(TOP_CITIES))

关于python - 从Python sql查询中的总和计算百分比/分数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46869864/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com