根据另一列的先前值填充一列

问题描述 投票:0回答:1

[我正在尝试创建一列,直到该交易完成的行为止填充每一行的交易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结尾。

enter image description here

基于下面的戈登答案,我也尝试过:

last_value(transactionid ignore nulls) over(
  order by hitnumber 
  rows between unbounded preceding and current row) as t_id2,

但是,这不会填充事件行,该事件行将使用交易ID(以下称为t_id2)进行交易:enter image description here

sql google-bigquery window-functions gaps-and-islands
1个回答
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);
© www.soinside.com 2019 - 2024. All rights reserved.