[英]Big query/sql to maintain the same data from next row onwards in the same column itself, got based on some condition in the current row
[英]Move each column value from the same group to new column SQL Big Query
考虑以下选项
select * from (
select *
from your_table,
unnest(split(name, ', ')) word with offset
)
pivot (min(word) as name for offset + 1 in (1, 2, 3))
如果应用于您问题中的示例数据
with your_table as (
select 'x1' id, 'yellow' name union all
select 'x2', 'orange' union all
select 'x3', 'pink, blue' union all
select 'x4', 'pink, blue, yellow'
)
output 是
这取决于您要拆分成新列的名称数量。 使用您共享的数据,您可以尝试以下查询:
SELECT DISTINCT t1.id, t1.name,
split(t1.name, ', ')[SAFE_offset(0)] as name_1,
split(t1.name, ', ')[SAFE_offset(1)] as name_2,
split(t1.name, ', ')[SAFE_offset(2)] as name_3
from `dataset.table` as t1
这带来了以下 output:
我使用SAFE_OFFSET
是因为如果你只使用OFFSET
id 1,2,3 将不允许它,因为它们会带来空值。
当名称列中有 NULL 时,如何保留行? 感谢您的帮助!
考虑以下
select * from (
select *
from your_table
left join unnest(split(name, ', ')) word with offset
)
pivot (min(word) as name for offset + 1 in (1, 2, 3))
如果应用于您问题中的示例数据
with your_table as (
select 'x1' id, 'yellow' name union all
select 'x2', 'orange' union all
select 'x3', 'pink, blue' union all
select 'x4', 'pink, blue, yellow' union all
select 'x5', null union all
select 'x6', null
)
output 是
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.