SELECT table1.id
, concat_ws('|',collect_list(table1.max_or_min_part)) as max_and_min_part
from
(
SELECT tab1.id as id
, if(tab2.min_part = tab1.col2,concat('min_part=',tab1.order_id),concat('max_part=',tab1.order_id))
as max_or_min_part
from
(
SELECT id
,order_id
,split(order_id,':')[0] as col1
,split(order_id,':')[1] as col2
from
(SELECT '1' as id
,concat("aa:11",",bb:22",",cc:33") as text
) tb1
lateral view explode(split(tb1.text,',')) num as order_id
)tab1
left join
(
SELECT tb2.id as id
,min(tb2.col2) as min_part
,max(tb2.col2) as max_part
from
(
SELECT t2.id
,order_id
,split(order_id,':')[0] as col1
,split(order_id,':')[1] as col2
from
(SELECT '1' as id
,concat("aa:11",",bb:22",",cc:33") as text
) t2
lateral view explode(split(t2.text,',')) num as order_id
)tb2
group by tb2.id
) tab2
on tab1.id = tab2.id
where tab1.col2 = tab2.max_part
or tab1.col2 = tab2.min_part
) table1
group by table1.id
;
hive sql 即hql行转列和列转行经典例子,无需自建数据
最新推荐文章于 2024-12-05 18:05:57 发布