[英]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.我想添加一个
new_column
(一个用逗号分隔的条目),它在 column2 值为 action_of_interest1 时添加/连接来自 column1 的条目,并在 column2 值为 ` action_of_interest1
时减去运行的连接数组。 For random_actions, do nothing just print whatever is the current array.对于 random_actions,什么也不做,只打印当前数组。
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?如何在 BigQuery/SQL 中执行此操作?
An approach using window functions:使用 window 函数的方法:
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: 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.