2016-12-11 44 views
4

我將Firebase事件導出到BigQuery,現在我試圖從某個事件中選擇兩個參數。這裏是用於選擇一個參數查詢:如何在BigQuery中選擇多個自定義Firebase事件參數?

select event_dim.params.value.int_value as level_id 
from [com_company_appname_ANDROID.app_events_20161210] 
where event_dim.name = "level_replays_until_first_victory" and event_dim.params.key = "level_id" 

兩個參數都是INT的值,所述第一參數的名稱爲level_id,並且第二參數是count。我想要顯示的第一列是level_id,第二列是count

回答

6

下面將情況與BigQuery Standard SQL

SELECT 
    (SELECT params.value.int_value FROM x.params 
           WHERE params.key = 'level_id') AS level_id, 
    (SELECT params.value.int_value FROM x.params 
           WHERE params.key = 'count') AS count 
FROM `com_company_appname_ANDROID.app_events_20161210`, UNNEST(event_dim) AS x 
WHERE x.name = 'level_replays_until_first_victory' 

參見Migrating from legacy SQL工作,如果你被卡住傳統的SQL

+0

謝謝!這工作完美。 –

+0

現在我們可以說我只想顯示「count = 5」的行,那麼在這種情況下,你會怎麼表達呢? –

+0

有很多方法。使我的回覆適合註釋 - 只需添加到查詢的最後一個突出顯示的語句AND(SELECT params.value.int_value FROM x.params WHERE params.key ='count')= 5'。如果你有更多的問題 - 請不要使用評論問 - 而是請發佈新的問題,我們將很樂意幫助 –

1

我喜歡以前的解決方案!這是我提出的同樣問題的另一種解決方案。我歡迎評論哪些解決方案更高效/更便宜,爲什麼。

SELECT event_param1.value.int_value AS level_id, 
event_param2.value.int_value AS count 
FROM `com_company_appname_ANDROID.app_events_20161210`, 
UNNEST(event_dim) event, 
UNNEST(event.params) as event_param1, 
UNNEST(event.params) as event_param2 
WHERE event.name = 'level_replays_until_first_victory' 
AND event_param1.key = 'level_id' 
AND event_param2.key = 'count' 
1

另一種解決方案,我覺得挺方便的就是使用User Defined Functions to analyze user properties and event parameters

#Standard-SQL 

#UDF for event parameters 
CREATE TEMP FUNCTION paramValueByKey(k STRING, params ARRAY<STRUCT<key STRING, value STRUCT<string_value STRING, int_value INT64, float_value FLOAT64, double_value FLOAT64 >>>) AS (
    (SELECT x.value FROM UNNEST(params) x WHERE x.key=k) 
); 

#UDF for user properties 
CREATE TEMP FUNCTION propertyValueByKey(k STRING, properties ARRAY<STRUCT<key STRING, value STRUCT<value STRUCT<string_value STRING, int_value INT64, float_value FLOAT64, double_value FLOAT64>, set_timestamp_usec INT64, index INT64 > >>) AS (
    (SELECT x.value.value FROM UNNEST(properties) x WHERE x.key=k) 
); 

#Query the sample dataset, unnesting the events and turn 'api_version', 'round' and 'type_of_game' into columns 
SELECT 
    user_dim.user_id, 
    event.name, 
    propertyValueByKey('api_version', user_dim.user_properties).string_value AS api_version, 
paramValueByKey('round', event.params).int_value as round, 
paramValueByKey('type_of_game', event.params).string_value as type_of_game 
FROM `firebase-analytics-sample-data.android_dataset.app_events_20160607`, 
UNNEST(event_dim) as event 
WHERE event.name = 'round_completed' 
LIMIT 10; 
相關問題