如图:
有四笔数据,但是是无序的,现在需要将其行转列成一行数据,并且里面的数据要按从小到大排序。
1.尝试先对这四笔数据进行排序,再进行行转列,但发现最终出来的数据还是达不到预期:
2.后百度到关于group_concat这个函数,其在mysql中,是可以进行对数据排序的,即group_concat(字段 order by 字段),但是在impala中不允许对行转列的内容进行排序。:
是否还有其他方式可以实现该需求,要求最终的结果是只有一行数据,数据为:2345的顺序。
嵌套一下,或者你写一个udf 函数更快
with temp as (
select '5' as id
union all
select '2' as id
union all
select '3' as id
union all
select '4' as id ),
rn as (select id,cast(row_number() over(order by id) as string) as rn from temp),
re as (select group_concat(concat_ws('',id,replace(rn,rn,''))) id from rn)
select *
from re;
或者
with temp as (
select t.*
from (
select '5' as id
union all
select '2' as id
union all
select '3' as id
union all
select '4' as id ) t
order by id
limit 4
)
select group_concat(id)
from temp;