1、创建表结构
create table test1
(s_name varchar(10),
s_class varchar(10),
c_value numeric
);
2、插入测试数据
insert into test1 values('张三','语文',78),('张三','数学',38),('张三','英语',89),('张三','化学',86),('张三','物理',68);
insert into test1 values('李四','语文',58),('李四','数学',67),('李四','英语',82),('李四','化学',56),('李四','物理',39);
3、查询数据
select s_name,s_class,c_value from test1 order by 1,2;
4、开启 tablefunc 行转列 crosstab 关键词
CREATE EXTENSION tablefunc;
5、编写行转列SQL
select * from crosstab('select s_name::text, s_class, c_value from test1 order by 1,2')
as ct (姓名 text,"化学" numeric,"数学" numeric,"物理" numeric,"英语" numeric,"语文" numeric)
最后修改于 2020-10-09 17:42:51
如果觉得我的文章对你有用,请随意赞赏
扫一扫支付

