SELECT e1 FROM(SELECT*FROM dwd_signal_securityindex_ri orderby time_stamp desclimit1)as t lateral view explode(approach_index) tmp1 as e1;
1
具体实现和SQL
原始数据
lateral view explode列转行
SELECT time_stamp, intersection_number, safety_factor,sub FROM dwd_signal_securityindex_ri as t lateral view explode ( approach_index ) tbl1 AS sub;
1
2
SPLIT_BY_STRING拆分字符串为数组
SELECT
time_stamp,
intersection_number,
safety_factor,
SPLIT_BY_STRING ( sub,'-') arr
FROM(SELECT time_stamp, intersection_number, safety_factor, sub FROM dwd_signal_securityindex_ri AS t lateral VIEW explode ( approach_index ) tbl1 AS sub ) aaa;
1
2
3
4
5
6
7
8
element_at获取数据
SELECT
time_stamp,
intersection_number,
element_at ( arr,1)AS approach,
element_at ( arr,2)AS pedestrianTimeGuaranteeRate ,
element_at ( arr,3)AS pedestrianIllegalRate ,
element_at ( arr,4)AS trafficConflict
FROM(SELECT
time_stamp,
intersection_number,
SPLIT_BY_STRING ( sub,'-') arr
FROM(SELECT time_stamp, intersection_number, safety_factor, sub FROM dwd_signal_securityindex_ri AS t lateral VIEW explode ( approach_index ) tbl1 AS sub ) aaa
) bbb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
创建视图
查询SQL写好后,可以创建视图,后续直接查询该视图即可
-- 路口进口级别,安全指标DROPVIEWIFEXISTS`signal`.dwd_signal_securityindex_ri_view;CREATEVIEW`signal`.dwd_signal_securityindex_ri_view ASSELECT
time_stamp,
intersection_number,
CAST(element_at ( arr,1)ASDECIMAL(2,0))AS approach,
CAST(element_at ( arr,2)ASDECIMAL(10,2))AS pedestrian_time_guarantee_rate ,
CAST(element_at ( arr,3)ASDECIMAL(10,2))AS pedestrian_illegal_rate ,
CAST(element_at ( arr,4)ASDECIMAL(5,0))AS traffic_conflict
FROM(SELECT
time_stamp,
intersection_number,
SPLIT_BY_STRING ( sub,'-') arr
FROM(SELECT time_stamp, intersection_number, safety_factor, sub FROM dwd_signal_securityindex_ri AS t lateral VIEW explode ( approach_index ) tbl1 AS sub ) aaa
) bbb