表格如下:
1. 方法一:
select pname,
sum(case when project = '身高' then tvalue else 0 end )as 身高 ,
String_agg(case when project = '身高' then unit else null end,'')as 身高单位 ,
sum(case when project = '体重' then tvalue else 0 end )as 体重 ,
String_agg(case when project = '体重' then unit else null end,'')as 体重单位 ,
sum(case when project = '年龄' then tvalue else 0 end )as 年龄 ,
String_agg(case when project = '年龄' then unit else null end,'')as 年龄单位
from healthexamination group by pname
order by 年龄 desc;
结果如下:
方法二:
select pname,String_agg(project||':'||tvalue||unit,',') from healthexamination group by pname;
结果如下:
方法三:
select pname,
split_part(split_part(temp,',',1),':',2)as 身高,
split_part(split_part(temp,',',1),':',3)as 身高单位,
split_part(split_part(temp,',',2),':',2)as 体重,
split_part(split_part(temp,',',2),':',3)as 体重单位,
split_part(split_part(temp,',',3),':',2)as 年龄,
split_part(split_part(temp,',',3),':',3)as 年龄单位
from(
select pname,String_agg(project||':'||tvalue||':'||unit,',')as temp from healthexamination group by pname)as t order by 年龄 desc;
结果如下