运行 在 Redshift 中计数

Running count in Redshift

我有一个活动 table 和一张门票(创建日期,person_id)table。当有人买票时,会在票中创建一行 table (Redshift)

我正在尝试做一个快照 table 这样我就可以看到过去任何一天在那个阶段购买了多少张票。

到目前为止我有这个

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

您似乎想要的是具有 window 函数的聚合:

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,但我还是保留了它(有一次,Redshift 需要 windowing 子句和 order by).