Select 如何在Google Big Query中使用SQL比较两个值?

Select 如何在Google Big Query中使用SQL比较两个值?,select,google-bigquery,firebase-cloud-messaging,countif,unnest,Select,Google Bigquery,Firebase Cloud Messaging,Countif,Unnest,我试图从Google Big Query数据库中获取在不同列中具有相同值的所有记录。比方说,当从手机发送一些事件时,我将变量machine\u name设置为firebaseuser\u properties。然后我将发送事件event\u notification\u send。当我查询表时,我想从数据库中获取所有数据,事件名为event\u notification\u send,它的参数machine\u name具有一些值X1,并且该记录必须同时在user\u properties中的键

我试图从Google Big Query数据库中获取在不同列中具有相同值的所有记录。比方说,当从手机发送一些事件时,我将变量
machine\u name
设置为firebase
user\u properties
。然后我将发送事件
event\u notification\u send
。当我查询表时,我想从数据库中获取所有数据,事件名为
event\u notification\u send
,它的参数
machine\u name
具有一些值X1,并且该记录必须同时在
user\u properties
中的键
Last\u notification
中具有一个参数,该参数的值为X1

如何进行SQL查询

谢谢

以下是我的代码示例:

#standardSQL
SELECT *
FROM 
`myProject.analytics_159820162.events_*`
WHERE
_TABLE_SUFFIX BETWEEN '20180725' AND '20180727' 
AND event_name in ("event_notification_received", "event_notification_dissmissed")
AND platform = "ANDROID"
AND
(SELECT COUNTIF((key = "machine_name")) 
              FROM UNNEST(event_params) 
) > 0  -- to see if specified event has such key
AND
(SELECT COUNTIF((key = "Last_notification")) 
              FROM UNNEST(user_properties) 
) > 0  -- to see if specified event has such key
ORDER BY event_timestamp ASC

要检查行/事件的参数“machine\u name”和“Last\u notification”是否具有相同的值,可以使用下面的语句

SELECT COUNT(DISTINCT key) cnt
FROM UNNEST(event_params) 
WHERE key IN ("machine_name", "Last_notification")
GROUP BY value
ORDER BY cnt DESC
LIMIT 1    
假设您的其余查询是正确的,下面将添加您的条件

#standardSQL
SELECT *
FROM `myProject.analytics_159820162.events_*`
WHERE _TABLE_SUFFIX BETWEEN '20180725' AND '20180727' 
AND event_name IN ("event_notification_received", "event_notification_dissmissed")
AND platform = "ANDROID"
AND (
    SELECT COUNT(DISTINCT key) cnt
    FROM UNNEST(event_params) 
    WHERE key IN ("machine_name", "Last_notification")
    GROUP BY value
    ORDER BY cnt DESC
    LIMIT 1
  ) = 2  
ORDER BY event_timestamp ASC   
注意:如果事件具有多个参数,且具有相同的键但值不同,则使用下面的方法只是为了安全起见

ORDER BY cnt DESC
LIMIT 1