Postgres:计算每周成功事件的百分比

时间:2016-11-05 00:54:47

标签: postgresql

对于我的数据集中的每个城市以及一周中的每一天,我需要确定2016年第一周注册的百分比,这导致在注册日期的168小时内完成旅行。

到目前为止,我可以用

计算每个城市的总体百分比
select cities.city_name, round( 100.0 * (sum(case when event_name='sign_up_success' then 1 end)) / count(events), 2) as percent from
trips inner join events on
trips.client_id = events.rider_id inner join cities on
trips.city_id = cities.city_id
group by city_name;

但是,当我尝试添加over (partition by date_trunc('day', event._ts))子句时,错误状态round不能用作聚合或窗口函数。

我能得到的最接近的是

select cities.city_name, 
      round( 100.0 * (sum(case when event_name='sign_up_success' then 1 end)) / count(events), 2) as percent 
from  trips 
inner join events 
   on trips.client_id = events.rider_id 
inner join cities 
   on  trips.city_id = cities.city_id
where trips.status = 'completed' 
  and events._ts < '2016-01-08' 
  and (trips.request_at - events._ts) < interval '168 hours'
group by cities.city_name;

我如何延长这一点,以便每天计算完成旅程的百分比? e.g。

Day  | City_1 | City_2
 1   | 45     | 25
 2   | 0      | 66
 3   | 100    | 25
 4   | 45     | 75
etc 

0 个答案:

没有答案