Redshift窗口功能的条件

时间:2017-07-24 20:43:39

标签: amazon-redshift

我正在尝试从包含user_idreceived_atevent_name列的表中识别每个用户的激活事件。要获得第一个事件,我可以使用以下窗口函数:

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

我试图获得具有特定名称的事件,在子选择中我可以做类似的事情

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

是否可以将这样的条件添加到窗口函数中?

1 个答案:

答案 0 :(得分: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