简体   繁体   中英

How to add a column which does a string addition of values from other column based on condition from another column

I have a table like this:

column1       column2
product1    action_of_interest1
product2    action_of_interest1
product3    random_action
product1    action_of_interest2

I want to add a new_column (an entry separated by comma) which adds/concatenates entries from column1 whenever column2 value is action_of_interest1 and subtracts the running concatenated array whenever column2 value is `action_of_interest2. For random_actions, do nothing just print whatever is the current array.

This is the resulting table:

column 1          column2                         new_column
product1     action_of_interest1                   product1
product2     action_of_interest1                  product1,product2
product3     random_action                    product1,product2
product1     action_of_interest2                  product2

How to do this in BigQuery/SQL?

An approach using window functions:

WITH sample AS (
  SELECT "product1" AS column1, "action_of_interest1" AS column2, 1 AS column3
  UNION ALL
  SELECT "product2" AS column1, "action_of_interest1" AS column2, 2 AS column3
  UNION ALL
  SELECT "product3" AS column1, "random_action" AS column2, 3 AS column3
  UNION ALL
  SELECT "product1" AS column1, "action_of_interest2" AS column2, 4 AS column3
),
running_agg as (
  SELECT
    *,
    ARRAY_AGG(IF(column2 = 'action_of_interest1', column1, '')) OVER (ORDER BY column3 ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) as action_of_interest1,
    ARRAY_AGG(IF(column2 = 'action_of_interest2', column1, '')) OVER (ORDER BY column3 ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) as action_of_interest2
  FROM sample
)
SELECT 
  * EXCEPT (action_of_interest1, action_of_interest2),
  ARRAY_TO_STRING(
    ARRAY(
      SELECT * FROM UNNEST(action_of_interest1) 
      EXCEPT DISTINCT 
      SELECT * FROM UNNEST(action_of_interest2)
    ), 
    ','
  ) AS new_column
FROM running_agg

Output:

column1     column2                 column3     new_column
product1    action_of_interest1     1           product1
product2    action_of_interest1     2           product1,product2
product3    random_action           3           product1,product2
product1    action_of_interest2     4           product2

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