2016-12-15 35 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。 。 。你應該提出新的問題作爲一個問題,而不是在評論中。 –