简体   繁体   中英

Filtering query by two values in same ARRAY or STRUCTs in Firebase Analytics BigQuery database

This question is a follow-up to this initial question .

In the previous example, we found how to filter an ARRAY of STRUCTs from Firebase's Analytics database.

Now here is the new challenge on top:

我们正在努力做什么

My thinking was, since we already did:

UNNEST(event_params) AS ep

This means that event_params is now flattened, so that means its values inside from the following ARRAYS:

  • value.string_value: can access values;
  • value.int_value: can access values;
  • etc..

So my thinking goes, since I managed to access value.string_value = 'Restaurant profile', it should be no problem to also access the other value you can see in the example called "restaurant_id", so I add some more AND statements:

  ep.key = 'restaurant_id' AND
  ep.value.int_value = 2045881 AND

But as you can imagine since I'm posting about it, the result was not quite what was expected even though technically BigQuery did accept my solution:

空结果

Any idea what is wrong in my initial query and how I can fix it?

Any idea what is wrong in my initial query and how I can fix it?

The problem was is that ep.key cannot be at the same time both firebase_screen and restaurant_id - so instead of using AND you need to use OR as in below example

SELECT *
FROM `diningcity-2ad82.analytics_171798853.events_20201222` e, UNNEST(e.event_params) ep 
WHERE e.platform = 'IOS' AND (
    (ep.key = 'firebase_screen' AND ep.value.string_value = 'Restaurant profile') OR 
    (ep.key = 'restaurant_id' AND ep.value.int_value = 2045881)
  )
LIMIT 1000;     

But what if I need both these conditions to be true? ...

I think in this case your initial query is not the best start - try below instead

SELECT *
FROM `diningcity-2ad82.analytics_171798853.events_20201222` e
WHERE e.platform = 'IOS' AND (
  SELECT COUNT(*)
  FROM e.event_params 
  WHERE
    (key = 'firebase_screen' AND value.string_value = 'Restaurant profile') OR 
    (key = 'restaurant_id' AND value.int_value = 2045881)
  ) = 2
LIMIT 1000;

You can use exists as follows:

SELECT *
FROM `diningcity-2ad82.analytics_171798853.events_20201222` e, 
      UNNEST(e.event_params) ep 
WHERE e.platform = 'IOS' 
  AND ep.key = 'firebase_screen'
  AND ep.value.string_value = 'Restaurant profile'
  And exists (select 1 from UNNEST(e.event_params) epp
               Where epp.key = 'restaurant_id' 
                 AND epp.value.int_value = 2045881)
LIMIT 1000;

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