BigQuery - добавляет элементы из массива в массив структур - PullRequest
0 голосов
/ 27 февраля 2019

У меня есть структура, которая выглядит следующим образом:

{"event": {
    "timestamp": [        
        "2019-01-13 17:21:08.570140 UTC",
        "2019-01-14 14:10:55.475515 UTC",
        "2019-01-09 14:02:51.848917 UTC"
    ],
    "properties": [
        {"device_model": "iPhone", "country": "United Kingdom"},
        {"device_model": "Android", "country": "United States"},
        {"device_model": "iPhone", "country": "Sweden"}
    ]
}

Я бы хотел добиться этого: чтобы каждая временная метка входила в соответствующую структуру.

{"event": [
        {"timestamp": "2019-01-13 17:21:08.570140 UTC","device_model": 
         "iPhone", "country": "United Kingdom"},
        {"timestamp": "2019-01-14 14:10:55.475515 UTC", "device_model": 
         "Android", "country": "United States"},
        {"timestamp": "2019-01-09 14:02:51.848917 UTC", "device_model": 
         "iPhone", "country": "Sweden"}
    ]
}

Я создал текущую структуру из запроса, подобного этому:

WITH
  events AS (
  SELECT
    "customer_1" AS customer_id,
    "timestamp_1" AS timestamp,
    STRUCT("iphone" AS device_model,
      "uk" AS country ) AS properties
  UNION ALL
  SELECT
    "customer_2" AS customer_id,
    "timestamp_2" AS timestamp,
    STRUCT("android" AS device_model,
      "us" AS country) AS properties
  UNION ALL
  SELECT
    "customer_2" AS customer_id,
    "timestamp_3" AS timestamp,
    STRUCT("iphone" AS device_model,
      "sweden" AS country) AS properties )
SELECT
  customer_id,
  STRUCT(ARRAY_AGG(timestamp) AS timestamp,
    ARRAY_AGG(properties) AS properties) AS event
FROM
  events
GROUP BY
  customer_id

Как я могу изменить запрос для получения желаемой структуры?

--- Edit

Я мог бы сделать это таким образом, но это требует знания схемы свойств в то время, когда я генерирую запрос - что возможно, но не очень красиво.Есть ли более простой способ?

WITH
  events AS (
  SELECT
    "customer_1" AS customer_id,
    "timestamp_1" AS timestamp,
    STRUCT("iphone" AS device_model,
      "uk" AS country ) AS properties
  UNION ALL
  SELECT
    "customer_2" AS customer_id,
    "timestamp_2" AS timestamp,
    STRUCT("android" AS device_model,
      "us" AS country) AS properties
  UNION ALL
  SELECT
    "customer_2" AS customer_id,
    "timestamp_3" AS timestamp,
    STRUCT("iphone" AS device_model,
      "sweden" AS country) AS properties )
SELECT
  customer_id,
  ARRAY_AGG(properties) AS event
FROM (
  SELECT
    customer_id,
    struct(timestamp as timestamp, 
           properties.device_model as device_model, 
           properties.country as country) as properties
  FROM
    events)
GROUP BY
  customer_id

1 Ответ

0 голосов
/ 27 февраля 2019

Вы могли бы сделать что-то вроде этого, используя SELECT AS STRUCT и используя properties в качестве селектора.

SELECT
  customer_id,
  ARRAY_AGG(properties) AS prop
FROM (
  SELECT
    customer_id,
    (
    SELECT
      AS STRUCT timestamp,
      properties.*) AS properties
  FROM
    events e )
GROUP BY
  1

это возвращает:

[
  {
    "customer_id": "customer_1",
    "prop": [
      {
        "timestamp": "timestamp_1",
        "device_model": "iphone",
        "country": "uk"
      }
    ]
  },
  {
    "customer_id": "customer_2",
    "prop": [
      {
        "timestamp": "timestamp_2",
        "device_model": "android",
        "country": "us"
      },
      {
        "timestamp": "timestamp_3",
        "device_model": "iphone",
        "country": "sweden"
      }
    ]
  }
]

Вы могли бы дополнительно написать кусок как:

ВЫБРАТЬ КАК СТРУКТУРУ e. * Кроме (customer_id)

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...