无法除以SQL中两个单独列表的计数,保持返回1

问题描述 投票:1回答:1

我有一个事件列表。一个事件名称是创建帐户,另一个是使用Facebook创建帐户。我试图看看创建的帐户占用了多少百分比。

下面的代码将给我一个Facebook帐户和总帐户数量的准确计数,但当我尝试划分这两个数字时,它只给我1号。

我是SQL的新手,并花了几个小时试图找出它为什么这样做无济于事。

with
fb_act as (
  select * 
  from raw_event 
  where name = 'onboard_fb_success' 
    and event_ts::date >= current_date - 30
),
total_act as (
  select * 
  from raw_event 
  where name ='create_account' 
    and event_ts::date >= current_date - 30
)    
select count(fb_act)/count(total_act), total_act.event_ts::date as day
from total_act, fb_act 
group by day 
order by day

我希望输出大约是〜.3,但实际输出总是精确为1。

sql postgresql
1个回答
0
投票

条件聚合是编写查询的一种更简单的方法。你似乎在使用Postgres,所以像这样:

select re.event_ts::date as day,
       (sum( (name = 'onboard_fb_success' and event_ts::date >= current_date - 30):: int) / 
        sum( name = 'create_account' and event_ts::date >= current_date - 30)::int)
       ) as ratio        
from raw_event re
group by re.event_ts::date
order by day;
© www.soinside.com 2019 - 2024. All rights reserved.