简体   繁体   中英

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

I have a table that looks like this:

record name1 name2 to_merge value1 value2
1 STEVE null false 30 null
2 JOHN null true 43 null
3 null LAURA true null 66
4 JEN null false 18 null

I want this to be the output:

record name1 name2 value1 value2
1 STEVE null 30 null
2 JOHN LAURA 43 66
3 JEN null 18 null

This means I want to merge the rows with a TRUE value in the to_merge field. 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

在此处输入图像描述

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