简体   繁体   English

如何根据 BigQuery 中的条件旋转/合并行?

[英]How to pivot/merge rows based on condition in BigQuery?

I have a table that looks like this:我有一个看起来像这样的表:

record记录 name1名称1 name2名称2 to_merge合并 value1价值1 value2价值2
1 1 STEVE史蒂夫 null null false错误的 30 30 null null
2 2 JOHN约翰 null null true真的 43 43 null null
3 3 null null LAURA劳拉 true真的 null null 66 66
4 4 JEN null null false错误的 18 18 null null

I want this to be the output:我希望这是 output:

record记录 name1名称1 name2名称2 value1价值1 value2价值2
1 1 STEVE史蒂夫 null null 30 30 null null
2 2 JOHN约翰 LAURA劳拉 43 43 66 66
3 3 JEN null null 18 18 null null

This means I want to merge the rows with a TRUE value in the to_merge field.这意味着我想在 to_merge 字段中合并具有 TRUE 值的行。 Any help is much appreciated!任何帮助深表感谢!

Consider below考虑下面

select * except(to_merge)
from your_table
where not to_merge
union all
select max(name1), max(name2),
  max(value1), max(value2)
from your_table
where to_merge            

if applied to data in your question - output is如果应用于您问题中的数据 - output 是

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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