简体   繁体   中英

BigQuery: Querying with standard sql

I have this table:

client_id   session_id  time    action  transaction_id  
------------------------------------------------------
1   1   15:01   view    NULL    
1   1   15:02   basket  NULL    
1   1   15:03   basket  NULL    
1   1   15:04   purchase    1   
1   2   15:05   basket  NULL    
1   2   15:06   purchase    2   
1   2   15:07   view    NULL    

And I want inside the session, for all the previous actions to register the transaction_id that occur for the first time (therefore at 15:03 transaction_id = NULL)

session_id  time    transaction_id  
------------------------------------
1   15:01   1   
1   15:02   1   
1   15:03   NULL    
1   15:04   1   
2   15:05   2   
2   15:06   2   
2   15:07   NULL    

Hmmm . . . assuming that there is only one transaction id per session, then you can use window functions:

select t.*,
       (case when row_number() over (partition by client_id, session_id, action
                                     order by time) = 1
             then max(transactc
ion_id) over (partition by client_id, session_id)
        end) as new_transaction_id
from t

Below is for BigQuery Standard SQL

#standardSQL
SELECT 
  client_id, session_id, time, action,
  (CASE 
    WHEN ROW_NUMBER() 
         OVER (PARTITION BY client_id, session_id, grp, action ORDER BY time) = 1
    THEN MAX(transaction_id) OVER (PARTITION BY client_id, session_id, grp) END
  ) AS transaction_id
FROM (
  SELECT *, 
    COUNTIF(transaction_id IS NOT NULL) 
      OVER(PARTITION BY client_id, session_id 
      ORDER BY time ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING) AS grp
  FROM YourTable
)
-- ORDER BY client_id, session_id, time  

You can test play with dummy data as below

#standardSQL
WITH YourTable AS (
  SELECT 1 AS client_id, 1 AS session_id, '15:01' AS time, 'view' AS action, NULL AS transaction_id UNION ALL
  SELECT 1, 1, '15:02', 'basket', NULL UNION ALL
  SELECT 1, 1, '15:03', 'basket', NULL UNION ALL
  SELECT 1, 1, '15:04', 'purchase', 1 UNION ALL
  SELECT 1, 1, '15:05', 'basket', NULL UNION ALL
  SELECT 1, 1, '15:06', 'basket', NULL UNION ALL
  SELECT 1, 1, '15:07', 'purchase', 3 UNION ALL
  SELECT 1, 2, '15:08', 'basket', NULL UNION ALL
  SELECT 1, 2, '15:09', 'purchase', 2 UNION ALL
  SELECT 1, 2, '15:10', 'view', NULL 
)
SELECT 
  client_id, session_id, time, action,
  (CASE 
    WHEN ROW_NUMBER() 
         OVER (PARTITION BY client_id, session_id, grp, action ORDER BY time) = 1
    THEN MAX(transaction_id) OVER (PARTITION BY client_id, session_id, grp) END
  ) AS transaction_id
FROM (
  SELECT *, 
    COUNTIF(transaction_id IS NOT NULL) 
      OVER(PARTITION BY client_id, session_id 
      ORDER BY time ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING) AS grp
  FROM YourTable
)
-- ORDER BY client_id, session_id, time  

Output is as expected

client_id   session_id  time    action      transaction_id   
1           1           15:01   view        1    
1           1           15:02   basket      1    
1           1           15:03   basket      null     
1           1           15:04   purchase    1    
1           1           15:05   basket      3    
1           1           15:06   basket      null     
1           1           15:07   purchase    3    
1           2           15:08   basket      2    
1           2           15:09   purchase    2    
1           2           15:10   view        null     

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM