[英]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.在前面的示例中,我们了解了如何从 Firebase 的 Analytics 数据库中过滤一个结构数组。
Now here is the new challenge on top:现在这是最重要的新挑战:
My thinking was, since we already did:我的想法是,因为我们已经这样做了:
UNNEST(event_params) AS ep UNNEST(event_params) 作为 ep
This means that event_params is now flattened, so that means its values inside from the following ARRAYS:这意味着 event_params 现在变平了,这意味着它的值来自以下 ARRAYS:
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:所以我的想法是,因为我设法访问了 value.string_value = 'Restaurant profile',所以访问另一个您可以在示例中看到的名为“restaurant_id”的值应该没问题,所以我添加了更多的 AND 语句:
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:但是正如您可以想象的那样,自从我发布它以来,结果并不完全是预期的,即使从技术上讲 BigQuery 确实接受了我的解决方案:
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问题是
ep.key
不能同时是firebase_screen
和restaurant_id
- 所以不是使用AND
你需要使用OR
如下例
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:您可以按如下方式使用
exists
:
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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.