Sql 红移运行计数
我有一个事件表和一个tickets(创建日期,person_id)表。当有人购买一张票时,会在tickets表中创建一行(红移) 我正在尝试创建一个快照表,以便查看过去任何一天在该阶段购买的门票数量 到目前为止,我有这个Sql 红移运行计数,sql,amazon-redshift,cumulative-sum,Sql,Amazon Redshift,Cumulative Sum,我有一个事件表和一个tickets(创建日期,person_id)表。当有人购买一张票时,会在tickets表中创建一行(红移) 我正在尝试创建一个快照表,以便查看过去任何一天在该阶段购买的门票数量 到目前为止,我有这个 select trunc(e.created), count(person_id) over (order by trunc(e.created) rows unbounded preceding) as cumulative_signups from even
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);
我认为
sum()
不需要前面的行无界(
),但我还是保留了它(在某一点上,Redshift需要带有order by
的windowing子句)。啊,是的,我尝试过这一点,但在sum中包装整个窗口函数,而不是第一部分,失败了。非常感谢。我也想填补空白——现在,没有票卖的日子已经错过了。现在正在使用RS生成日期序列,并将其加入到@Pythonn00b中。你应该把新问题作为一个问题而不是在评论中提出。