0

I have integrated Firebase analytics data with google BigQuery and everyday a new table gets created with the DATE stamp.

one sample table is "projectID.com_dev_sambhav_ANDROID.app_events_20170821"

One sample table is shown below sample table

My requirement is to get result in the below desired format for event_dim.name="notification_received" desired output

To get this desired output I execute the below query(standardSQL)

SELECT event.name
 (SELECT param.value.string_value FROM UNNEST(event_dim.params) AS param WHERE param.key="notification_title") as notification_title,
 (SELECT param.value.string_value FROM UNNEST(event_dim.params) AS param WHERE param.key="item_id") as item_id 
FROM `projectID.com_dev_sambhav_ANDROID.app_events_20*`, UNNEST(event_dim) as event  
WHERE event.name = "notification_received"

But I got this error

Error: Each function argument is an expression, not a query. To use a query as an expression, the query must be wrapped with additional parentheses to make it a scalar subquery expression.

can anybody help me get out of this problem

KENdi
  • 7,576
  • 2
  • 16
  • 31
  • 1
    Please Debiprasad, whenever possible try using text instead of images in your questions as google can index the text and it's easier for whomever is reading your question to follow through the information. – Willian Fuks Aug 23 '17 at 10:23

1 Answers1

1

The problem is a comma and an alias. This query works:

#standardSQL
WITH `projectID.com_dev_sambhav_ANDROID.app_events_2017` AS(
  SELECT ARRAY< STRUCT<date STRING, name STRING, params ARRAY< STRUCT<key STRING, value STRUCT<string_value STRING> > > > > [STRUCT('20170814' AS date, 'notification_received' AS name, [STRUCT('notification_title' AS key, STRUCT('Amazing Offers two' AS string_value) AS value ), 
                                                                                                                STRUCT('firebase_screen_class' AS key, STRUCT('RetailerHomeActivity' AS string_value) AS value),
                                                                                                                STRUCT('notification_id' AS key, STRUCT('12345' AS string_value) AS value),
                                                                                                                STRUCT('firebase_screen_id' AS key, STRUCT('app' AS string_value) AS value),
                                                                                                                STRUCT('item_id' AS key, STRUCT('DEMO-02' AS string_value) AS value),
                                                                                                                STRUCT('firebase_screen' AS key, STRUCT('My Order' AS string_value) AS value)] AS params)] event_dim
)

SELECT
 event.name,
 (SELECT param.value.string_value FROM UNNEST(event.params) AS param WHERE param.key="notification_title") as notification_title,
 (SELECT param.value.string_value FROM UNNEST(event.params) AS param WHERE param.key="item_id") as item_id 
FROM `projectID.com_dev_sambhav_ANDROID.app_events_20*`, UNNEST(event_dim) as event  
WHERE event.name = "notification_received"

If you UNNEST the field event_dim and call it event, then you should use this alias as reference in your query.

As a complement, here's another way of solving your problem as well (it's just another possibility so you have more techniques in your belt when working with BigQuery):

#standardSQL
SELECT
  (SELECT date FROM UNNEST(event_dim)) date,
  (SELECT params.value.string_value FROM UNNEST(event_dim) event, UNNEST(event.params) params WHERE event.name = 'notification_received' AND params.key = 'notification_title') AS notification_title,
  (SELECT params.value.string_value FROM UNNEST(event_dim) event, UNNEST(event.params) params WHERE event.name = 'notification_received' AND params.key = 'item_id') AS item_id
FROM `projectID.com_dev_sambhav_ANDROID.app_events_2017`
WHERE EXISTS(SELECT 1 FROM UNNEST(event_dim) WHERE name = 'notification_received')

When processing up to terabytes you may find this query still perform quite well.

Willian Fuks
  • 11,259
  • 10
  • 50
  • 74
  • But Then, i used the same query for another event "notification_opened" and it threw error. – Debiprasad Mishra Aug 23 '17 at 10:46
  • ` SELECT (SELECT date FROM UNNEST(event_dim)) date, (SELECT params.value.string_value FROM UNNEST(event_dim) event, UNNEST(event.params) params WHERE event.name = 'notification_opened' AND params.key = 'notification_title') AS notification_title, (SELECT params.value.string_value FROM UNNEST(event_dim) event, UNNEST(event.params) params WHERE event.name = 'notification_opened' AND params.key = 'item_id') AS item_id FROM `shikhar-fa2b7.com_hul_sambhav_ANDROID.app_events_20*` WHERE EXISTS(SELECT 1 FROM UNNEST(event_dim) WHERE name = 'notification_opened')` – Debiprasad Mishra Aug 23 '17 at 10:47
  • error: Scalar subquery produced more than one element – Debiprasad Mishra Aug 23 '17 at 10:49
  • This happens when the sub-query finds several values with the same `param.value.string_value`. One way of solving it could be to bring the result as an ARRAY like `ARRAY((select params.value.string_value...)` but this would change the structure of your data. Let me know if this works for you. – Willian Fuks Aug 23 '17 at 10:59
  • I used the first suggestion ( WITH table AS) and it worked for notification_opened. Thanks Bro – Debiprasad Mishra Aug 23 '17 at 11:13