在bigquery中查找多个值的最小值和最大值

时间:2017-05-27 20:40:42

标签: sql google-bigquery

我的bigquery表中有“add_road”事件,其中包含距离和时间参数的int值。我想找到最大和最小的参数“距离”和“时间”。我已经尝试过这个查询,但它无法正常工作。

SELECT
  (SELECT MAX(param.value.double_value)
  FROM UNNEST (event.params) AS param
  WHERE param.key = "distance") AS distance,
  (SELECT MAX(param.value.int_value)
  FROM UNNEST (event.params) AS param
  WHERE param.key = "time") AS time
FROM 
  `ukrbikeapp.info_androidhive_firebase_ANDROID.app_events_*`,
  UNNEST(event_dim) as event
WHERE event.name = "add_road"
  AND (_TABLE_SUFFIX LIKE '201_05__')

我不知道如何进行此查询。请给我一些建议。

2 个答案:

答案 0 :(得分:2)

看起来您希望将MAX的计算移动到外部选择列表,以便您可以在所有行中找到最大值,而不仅仅是在数组中。例如,

SELECT
  MAX((SELECT param.value.double_value
  FROM UNNEST (event.params) AS param
  WHERE param.key = "distance")) AS distance,
  MAX((SELECT param.value.int_value
  FROM UNNEST (event.params) AS param
  WHERE param.key = "time")) AS time
FROM 
  `ukrbikeapp.info_androidhive_firebase_ANDROID.app_events_*`,
  UNNEST(event_dim) as event
WHERE event.name = "add_road"
  AND (_TABLE_SUFFIX LIKE '201_05__')

答案 1 :(得分:1)

以下是BigQuery Standard SQL

  
#standardSQL
SELECT 
  MAX(IF(param.key = "distance", param.value.double_value, NULL)) AS distance,
  MAX(IF(param.key = "time", param.value.int_value, NULL)) AS time
FROM data, UNNEST(event_dim) AS event, UNNEST(event.params) AS param
WHERE event.name = "add_road"
AND (_TABLE_SUFFIX LIKE '201_05__')