I gather analytics with Firebase Analytics which I linked to Google BigQuery.
I have the following data in BigQuery (unnecessary columns/rows are left off, the dataset looks similar to https://bigquery.cloud.google.com/table/firebase-analytics-sample-data:ios_dataset.app_events_20160607?tab=preview):
| event_dim.name | event_dim.params.key | event_dim.params.value.string_value |
|----------------|----------------------|-------------------------------------|
| read_post | post_id | p_100 |
| | group_id | g_1 |
| | user_id | u_1 |
| open_group | post_id | p_200 |
| | group_id | g_2 |
| | user_id | u_1 |
| open_group | post_id | p_300 |
| | group_id | g_1 |
| | user_id | u_3 |
I want to query the following data:
- event name
- user id
- group id
I tried the following query:
SELECT
event_dim.name,
FIRST(IF(event_dim.params.key = "user_id", event_dim.params.value.string_value, NULL)) WITHIN RECORD user_id,
FIRST(IF(event_dim.params.key = "group_id", event_dim.params.value.string_value, NULL)) WITHIN RECORD group_id
FROM
[xxx:xxx_IOS.app_events_20161102]
LIMIT
1000
The problem with the above query is that the aggregate function FIRST
will give the wrong result because the SELECT
statements with a WITHIN
modifier will return a list of results. The FIRST
function will only give the correct result in case of the first row.