Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
441 views
in Technique[技术] by (71.8m points)

sql - 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:

What we are trying to do

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:

Empty results

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...