I'm trying to make a BigQuery query to extract data from the firebase db sub-table event_params.
The tables events_* have a list of events in which the field event_params is a struct that contains a table in the form of key, int_value and other fields not used in this case.
For example, 1 single event may have a list of event_params like this:
key int_value
level 1
time 35
kills 10
I'd like to filter the list of events by the level but select the time from that event. The solution I got is this:
select user_pseudo_id, x.value.int_value as time
from (
select user_pseudo_id, event_params
from `whatever.events_*`, unnest(event_params) as y
where event_name = 'level_complete' and y.key = 'level' and y.value.int_value = 1
), unnest(event_params) as x
where x.key = 'time'
It works for this case but I have a few questions on how to improve and make this grow:
- Is there a way of simplifying this in one single query?
- What if I want to get the time field but also the kills field? (The only solution I can think of is duplicating the query and joining both results, but what if I want to extract a lot of fields?).
- Can this be simplified by creating something like a function that returns a resulting table like
Filter('level_complete', 'level', 1)?
Thanks in advance!