2017-10-22 47 views
0

我有一个查询,可以获得一个城市的投诉数量。从蟒蛇SQL查询中计算总和的百分比/分数

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)) 

ComplaintType CITY_NAME .Complain_

现在,我想创建一个列计算类型t发生在城市的投诉。它会像count(ComplaintType)/ sum(count(ComplaintType)在城市)

什么是最好的语法来完成这个?

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

'计数(ComplaintType)/(从数据SELECT COUNT(*))'我想。 –

回答

1

嗯,一种方法是在子查询中总结并在连接的结果:

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)) 
+0

我明白这一点。你碰巧知道为什么我会得到一个错误:没有这样的列:city_name? – GenXeral

+0

nvm。我将cc部分更改为cc.city_name – GenXeral

+0

代码中没有错误,但由于某些原因,我的所有比率均为0。 – GenXeral