2016-12-15 63 views
2

我有一个事件表和一张门票(创建日期,person_id)表。当有人购买一张票时,会在票据表中创建一行(Redshift)在Redshift中运行计数

我试图做一张快照表,以便我可以看到在过去的任何一天在该阶段购买了多少张票。

到目前为止,我有这个

select 
    trunc(e.created), 
    count(person_id) over (order by trunc(e.created) rows unbounded preceding) as cumulative_signups 
from event e 
LEFT JOIN person_tickets t on e.id = t.event_id 

问题是这样给我排每注册,这意味着我得到这个,而不是每天一行。

trunc  cumulative_signups 
2016-01-15 1 
2016-01-15 2 
2016-01-15 3 
2016-01-15 4 
2016-01-16 5 



trunc  cumulative_signups 
2016-01-15 4 
2016-01-16 5 

回答

3

你似乎什么需要的是聚集与窗口函数:

select trunc(e.created), count(*) as day_count, 
     sum(count(*)) over (order by trunc(e.created) rows unbounded preceding) as cumulative_signups 
from event e left join 
    person_tickets t 
    on e.id = t.event_id 
group by trunc(e.created) 
order by trunc(e.created); 

我不认为需要为sum()rows unbounded preceding,但我把它反正(在一个点上,红移需要与order by开窗条款)。

+0

啊是的,我已经尝试过这一点,但是将整个窗口函数包装在Sum中,而不是仅仅失败的第一部分。非常感谢。 – Pythonn00b

+0

我还想填补空白 - 现在,没有售票的日子都错过了。现在正在RS中生成一个日期序列并加入。 – Pythonn00b

+1

@ Pythonn00b。 。 。你应该提出新的问题作为一个问题,而不是在评论中。 –