我正在尝试创建一列,该列填充直到交易完成的行之前的每一行的交易ID-在此示例中,订单前的每个“添加到购物篮”事件。
到目前为止,我已经尝试使用FIRST_VALUE
:
SELECT
UserID, date, session_id, hitnumber, add_to_basket, transactionid,
first_value(transactionid) over (partition by trans_part order by date, transactionid) AS t_id
FROM(
select UserID, date, session_id, hitnumber, add_to_basket, transactionid,
SUM(CASE WHEN transactionid IS NULL THEN 0 ELSE 1 END) OVER (ORDER BY date, transactionid) AS trans_part,
FIRST_VALUE(transactionid IGNORE NULLS)
OVER (PARTITION BY userid ORDER BY hitnumber ASC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS t_id,
from q1
join q2 using (session_id)
order by 1,2,3,4
)
但是我得到的结果却是我想要的结果,将上一个订单的交易ID与该交易之后发生的购物篮事件相对应。
如何更改代码,以便在导致该事件的购物篮事件发生后看到订单的交易ID?例如,在下表中,我想查看t_id
列的事务ID以... 095结尾,而不是ID以... 383结尾。
基于下面的戈登答案,我也尝试过:
last_value(transactionid ignore nulls) over(
order by hitnumber
rows between unbounded preceding and current row) as t_id2,
答案 0 :(得分:1)
您可以使用last_value(ignore nulls)
:
select . . . ,
last_value(transaction_id ignore nulls) over (
order by hitnumber
rows between unbounded preceding and current row
) as t_id
from q1 join
q2 using (session_id);
与您的答案不同的是在当前行结束的windowing子句。
编辑:
每个t_id
似乎有一个session_id
,因此只需使用max()
:
select . . . ,
max(transaction_id) over (partition by session_id) as t_id
from q1 join
q2 using (session_id);