【发布时间】:2021-01-07 10:25:27
【问题描述】:
我有一个嵌套的 JSON 结构,我想在其中取消嵌套一个 JSON 子树。 A 或 B 都已填充,并且 event.type 标记为“A”或“B”。下面是两个简化的例子:
{
"event": {
"event_type": "A",
"time": 1599692445083,
"A" : {
"name": "item1",
"revenue": 100
}
}
}
}
{
"event": {
"event_type": "B",
"time": 1599692445083,
"B" : {
"items" : [
{"name": "item2", revenue" : 10},
{"name": "item3", revenue" : 20},
]
}
}
}
}
查询到目前为止有效,但我有一个问题,即 UNNEST 仅适用于交叉连接,因此我丢失了所有“A”事件,因为我需要左连接。请注意,我进行了多次聚合,因此以更简单的方式单独计算总和就足够了。
select
from_unixtime( (floor(event.time/1000) / (60 * 60)) *60*60) as event_hour,
count(*) filter(where event.event_type = 'A') as A_items,
count(*) filter(where event.event_type = 'B') as B_items
FROM mydb.event_table
left join unnest(event.B.items) as t(b)
WHERE
year=2020 and month=9 and day=18 and hour=1
GROUP BY
from_unixtime( (floor(event.time/1000) / (60 * 60)) *60*60),
对于 cross join unnest 查询执行,但由于 A 没有项目数组,我没有计算任何条目。对于 left join unnest,我收到了一条错误消息(很有趣,它在那里也声明了“left”):
mismatched input 'where' expecting {'join', 'cross', 'inner', 'left', 'right', 'full', 'natural', 'using', 'on', 'tablesample'}
有没有办法在 Athena 的 unnest 中使用左连接?
【问题讨论】:
-
我实际上并不确定您是否需要整个 unnest,这不可行吗?
count(*) filter(where event.event_type = 'A') as A_items, sum(cardinality(event.B.items)) filter(where event.event_type = 'B') as B_items
标签: arrays json left-join presto amazon-athena