[英]BigQuery - Add elements from array into array of structs
我有一个看起来像这样的结构:
{"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
如何修改查询以获得所需的结构?
- - 编辑
我可以这样做,但这需要在我生成查询时知道属性的模式 - 这是可能的,但不是很漂亮。 有没有更简单的方法?
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
您可以利用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"
}
]
}
]
你可以进一步写下这样的文章:
SELECT AS STRUCT e.* except(customer_id)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.