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

时间:2020-04-02 10:40:48

标签: sql google-bigquery window-functions gaps-and-islands

我正在尝试创建一列,该列填充直到交易完成的行之前的每一行的交易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

1 个答案:

答案 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);