简体   繁体   中英

How can I filter a BigQuery dataset that contains array data?

I have a query that contains array data. What I need to do is perform queries that can filter the results of the following screen, like:

"bring me the data for player alex". This should bring 2 rows:

1. user1@example.com, 2021-01-13 09:32:55.113 UTC, alex, 4, [8,10]
2. user1@example.com, 2021-01-13 09:30:07.572 UTC, alex, 10, [13,14]

具有数组数据的 BigQuery 数据集

how can such query be structured?

Try the followin in standard SQL in BigQuery:

with data as (
  select    "email@email.com" as email, CURRENT_TIMESTAMP() as timestamp,
            ['player1', 'alex', 'p3'] as player, [4, 4, 4] as must_respond_in, ['2, 9', '8, 10', '7'] as responses
  UNION ALL
  select    "email@email.com" as email, CURRENT_TIMESTAMP() as timestamp,
            ['player1', 'alex', 'p3'] as player, [14, 10, 14] as must_respond_in, ['1, 16', '13, 14', '0, 0'] as responses
)
select  email, timestamp, player[offset(x)],
        must_respond_in[offset(x)], responses[offset(x)]
from data, unnest(data.player) as player_name with offset x where player_name = 'alex';

I used WITH OFFSET clause to select elements that I need. WITH clause is used to simulate your data.

Yet another option

select  email, timestamp, 
  player,
  must_respond_in, 
  responses
from data t, t.player as player with offset
left join t.must_respond_in as must_respond_in with offset using(offset)
left join t.responses as responses with offset using(offset)
where player = 'alex'   

if to apply to sample 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