我有一个事件表和一个票证(创建日期,person_id)表。有人买票时,会在票表中创建一行(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
最佳答案
您似乎想要的是使用窗口函数进行聚合:
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);
我认为
rows unbounded preceding
不需要sum()
,但无论如何我还是把它留了下来(有一点,Redshift要求windowing子句带有order by
)。关于sql - Redshift中的运行计数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41163672/