如何加入雅典娜的unnest功能?
How to join with unnest function in athena?
我在 Athena 上有这个查询
trip.id as tripid
, segment.id as segmentid
, segment.distance as mileage
, segment.maxspeed as maxspeed
, segment.duration as duration
, segment.roadtype as roadtype
, segment.timeslotdata as timeslots
, extract( week from (from_unixtime(trip.referencedate /1000))) as weekyear
, extract( year from (from_unixtime(trip.referencedate /1000))) as year
, extract( month from (from_unixtime(trip.referencedate/1000))) as month
, unn.firstpositionlat
, unn.firstpositionlong
from
trip
, UNNEST(segments) as t(segment)
left join
(
select
position.latitude as firstpositionlat
, position.longitude as firstpositionlong
, position.id as id
from
trip
, UNNEST(segments) as t(segment)
, UNNEST(segment.positions) as t(position)
where
anno = 2021
and mese = 01
and giorno = 01
and tscid = 'XXX'
)
unn
on
segment.startpositionid = unn.id
where
anno = 2021
and mese = 01
and giorno = 01
and tscid = 'XXX'
问题是我无法加入,因为在线 16:19 出现错误,我遇到了这个错误
SYNTAX_ERROR: line 16:19: Column 'segments' cannot be resolved
我想不通问题出在哪里。
没有 ON 语句,这个效果很好。
提前致谢
Presto(Athena 的基础 SQL 引擎)仅在 CROSS JOIN
的右侧支持 UNNEST
。例如:
-- sample data
WITH dataset (id, animals) AS (
values (1, ARRAY['dog', 'cat', 'bird']),
(2, ARRAY['cow', 'pig'])
)
-- query
SELECT id, animals, a
FROM dataset
CROSS JOIN UNNEST(animals) AS t (a);
输出:
id
animals
a
1
[dog, cat, bird]
dog
1
[dog, cat, bird]
cat
1
[dog, cat, bird]
bird
2
[cow, pig]
cow
2
[cow, pig]
pig
似乎 shorthand 表示法 (from trip, UNNEST(segments) as t(segment)
) 在后跟另一个带有 on
条件的连接时无法正常工作,请尝试将其扩展为完整形式:
from trip
cross join UNNEST(segments) as t(segment)
left join (...)
on ...
我在 Athena 上有这个查询
trip.id as tripid
, segment.id as segmentid
, segment.distance as mileage
, segment.maxspeed as maxspeed
, segment.duration as duration
, segment.roadtype as roadtype
, segment.timeslotdata as timeslots
, extract( week from (from_unixtime(trip.referencedate /1000))) as weekyear
, extract( year from (from_unixtime(trip.referencedate /1000))) as year
, extract( month from (from_unixtime(trip.referencedate/1000))) as month
, unn.firstpositionlat
, unn.firstpositionlong
from
trip
, UNNEST(segments) as t(segment)
left join
(
select
position.latitude as firstpositionlat
, position.longitude as firstpositionlong
, position.id as id
from
trip
, UNNEST(segments) as t(segment)
, UNNEST(segment.positions) as t(position)
where
anno = 2021
and mese = 01
and giorno = 01
and tscid = 'XXX'
)
unn
on
segment.startpositionid = unn.id
where
anno = 2021
and mese = 01
and giorno = 01
and tscid = 'XXX'
问题是我无法加入,因为在线 16:19 出现错误,我遇到了这个错误
SYNTAX_ERROR: line 16:19: Column 'segments' cannot be resolved
我想不通问题出在哪里。 没有 ON 语句,这个效果很好。
提前致谢
Presto(Athena 的基础 SQL 引擎)仅在 CROSS JOIN
的右侧支持 UNNEST
。例如:
-- sample data
WITH dataset (id, animals) AS (
values (1, ARRAY['dog', 'cat', 'bird']),
(2, ARRAY['cow', 'pig'])
)
-- query
SELECT id, animals, a
FROM dataset
CROSS JOIN UNNEST(animals) AS t (a);
输出:
id | animals | a |
---|---|---|
1 | [dog, cat, bird] | dog |
1 | [dog, cat, bird] | cat |
1 | [dog, cat, bird] | bird |
2 | [cow, pig] | cow |
2 | [cow, pig] | pig |
似乎 shorthand 表示法 (from trip, UNNEST(segments) as t(segment)
) 在后跟另一个带有 on
条件的连接时无法正常工作,请尝试将其扩展为完整形式:
from trip
cross join UNNEST(segments) as t(segment)
left join (...)
on ...