postgres/greenplum unnest(Array) 实现列转行

1.创建测试表

create table a_test_col(t varchar(20),a1 int,a2 int,a3 int)

insert into a_test_col
select 'A',1,2,3
union all
select 'B',0,9,5

 

查询该表

SELECT * FROM a_test_col

 

2.通过 unnest(Array) 实现列转行

select T, -- 聚合列
unnest(Array['a1','a2','a3']) as colname, -- 转换的列明 col
unnest(Array[a1,a2,a3]) as colvalue -- 转换的列内容 colvalue
FROM a_test_col

 

 

posted @ 2021-05-21 12:15  ZHOUZC  阅读(1422)  评论(0编辑  收藏  举报