重庆思庄Oracle、Redhat认证学习论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 478|回复: 0
打印 上一主题 下一主题

使用array_agg实现 PG16 下面的行变列

[复制链接]
跳转到指定楼层
楼主
发表于 2023-12-1 16:59:07 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式


postgres=# create table test(id int,v int);
CREATE TABLE
postgres=# insert into test values(1,1);
INSERT 0 1
postgres=# insert into test values(1,2);
INSERT 0 1
postgres=# insert into test values(1,3);
INSERT 0 1
postgres=# insert into test values(2,20);
INSERT 0 1
postgres=# insert into test values(2,21);
INSERT 0 1
postgres=# insert into test values(2,22);
INSERT 0 1
postgres=# insert into test values(3,31);
INSERT 0 1
postgres=# insert into test values(3,32);
INSERT 0 1
postgres=# insert into test values(3,33);
INSERT 0 1
postgres=# select * from test;
id | v  
----+----
  1 |  1
  1 |  2
  1 |  3
  2 | 20
  2 | 21
  2 | 22
  3 | 31
  3 | 32
  3 | 33
(9 rows)


postgres=# select id,array_agg(v) from test group by id;
id | array_agg  
----+------------
  3 | {31,32,33}
  2 | {20,21,22}
  1 | {1,2,3}
(3 rows)





分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 支持支持 反对反对
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|手机版|小黑屋|重庆思庄Oracle、Redhat认证学习论坛 ( 渝ICP备12004239号-4 )

GMT+8, 2024-4-30 04:02 , Processed in 0.090461 second(s), 19 queries .

重庆思庄学习中心论坛-重庆思庄科技有限公司论坛

© 2001-2020

快速回复 返回顶部 返回列表