使用大查询,如何在记录中查询多个对象?

时间:2018-06-20 19:16:39

标签: google-analytics google-bigquery firebase-analytics

下面是我正在使用的结构。这是来自Firebase的Google Analytic事件。

我需要AND遍历params数组,并仅使用'eventData'键返回一个param值。同时,我需要将查询限制为在同一params数组中找到的两个其他对象(键值分别为“ peripheral”和“ eventType”)。

我正在使用旧版SQL,但愿意与其他产品一起使用。我看着并与FLATTEN一起玩,但这似乎并没有给我最终想要的东西。 查询示例可能更有意义。

    SELECT event_dim.params.value.int_value -- this needs to come from the eventData object
    FROM 
   [table]
    where event_dim.name = 'EPILOG_BAT'
    AND event_dim.timestamp_micros > 1529423373498009
    -- AND event_dim.params contains (key='peripheral', string_value = 'CA5D6D4C-9B4C-9BB1-03CF-5A8BCD7651FE')
    -- AND event_dim.params contains (key='eventType', string_value = 'V')
    order by event_dim.timestamp_micros ,
    event_dim.params.key

我希望得到的行是 4105

数据:

 "event_dim": [
  {{
    "date": "20180620",
    "name": "EPILOG_BAT",
    "params": [

      {
        "key": "eventData",
        "value": {
          "string_value": null,
          "int_value": "4105",
          "float_value": null,
          "double_value": null
        }
      },
      {
        "key": "peripheral",
        "value": {
          "string_value": "CA5D6D4C-9B4C-9BB1-03CF-5A8BCD7651FE",
          "int_value": null,
          "float_value": null,
          "double_value": null
        }
      },
      {
        "key": "eventTime",
        "value": {
          "string_value": null,
          "int_value": "1529499380000",
          "float_value": null,
          "double_value": null
        }
      },
      {
        "key": "eventType",
        "value": {
          "string_value": "V",
          "int_value": null,
          "float_value": null,
          "double_value": null
        }
      },
      {
        "key": "firmwareVersion",
        "value": {
          "string_value": "0.8.6\u0000\u0000\u0000\u0000\u0000\u0000\u0000",
          "int_value": null,
          "float_value": null,
          "double_value": null
        }
      }
    ],
    "timestamp_micros": "1529478561132285",
    "previous_timestamp_micros": "1529478560856003",
    "value_in_usd": null
  },}

1 个答案:

答案 0 :(得分:0)

也许是这样的(请问我的格式):

#standardSql
SELECT
(SELECT params.value.int_value FROM event.params WHERE params.key = 
'eventData') AS id
FROM `data_source`, 
UNNEST(event_dim) AS event,
UNNEST(event.params) AS params
WHERE event.name = 'EPILOG_BAT'
AND event_dim.timestamp_micros > 1529423373498009
AND (SELECT params.value.string_value FROM event.params WHERE params.key = 
'peripheral') = 'CA5D6D4C-9B4C-9BB1-03CF-5A8BCD7651FE'
AND (SELECT params.value.string_value FROM event.params WHERE params.key = 
'eventType') = 'V'