Skip to content
Advertisement

How to query a struct in BigQuery where two key: value pairs matter

I have data like this:

date, event.key, event.values
1   ,     color,         blue
           size,          big
2   ,     color,       yellow
           size,          big
3   ,     color,       yellow
           size,        small

And want to filter rows which contain such key:value pairs: color=blue and size=big. So only this one row remains:

date, event.key, event.values
1   ,     color,         blue
           size,          big

Advertisement

Answer

You can use unnest():

select t.*
from t
where exists (select 1
              from unnest(t.event) e
              where e.key = 'color' and e.value = 'blue'
             ) and
      exists (select 1
              from unnest(t.event) e
              where e.key = 'size' and e.value = 'big'
             );     
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement