2017-07-24 32 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