2017-07-24 71 views
0

我試圖從列user_id,received_atevent_name列表中識別每個用戶的激活事件。爲了得到第一個事件,我可以使用下面的窗口函數:我可以做類似Redshift窗口函數中的條件

MIN(
CASE WHEN 
e.event_name = 'User Activated' 
THEN e.received_at 
ELSE NULL END 
) AS user_activated 

first_value(e.received_at ignore nulls) 
over (partition by e.user_id 
order by e.received_at rows between unbounded preceding and unbounded following) 
as first_event 

我試圖得到的只是具有特定名稱的事件,子查詢中有可能像這樣的條件添加到窗口函數?

+1

爲什麼不呢,你試過嗎? 'first_value(case e.event_name ='User Activated'then e.received_at end ignore Nulllls)' – AlexYes

+0

@AlexY你說得對。對不起,問一個愚蠢的問題,仍然包裹着我的頭。隨意添加它作爲答案,我會接受它。 – KMV

回答

1

窗口功能允許不只列但作爲參數的任何表情,這樣你就可以很容易地使用CASE有:

first_value(case 
    when e.event_name = 'User Activated' 
    then e.received_at 
    end 
ignore nulls) 
over (
    partition by e.user_id 
    order by e.received_at 
    rows between unbounded preceding and unbounded following) 
as first_event