列转行

postgresql列转行的思路主要是利用string_to_array进行数组转换,然后用unnest进行行拆分

select t.bid_unit,unit_id from unit t
where t.unit_id=1947;result=> 中国信息通信研究院;北京市海淀区学院-- by zhengkai.blog.csdn.net

select unnest(string_to_array(t.bid_unit,';')),unit_id from unit t
where t.unit_id=1947;result=>
中国信息通信研究院
北京市海淀区学院-- by zhengkai.blog.csdn.net

pgsql官方对functions-array的解释

Function Return Type Description Example Result
string_to_array(text, text [, text]) text[] splits string into array elements using supplied delimiter and optional null string (使用提供的分隔符和可选的空字符串将字符串分割为数组元素) string_to_array(‘xx^yy^zz’, ‘^’, ‘yy’) {xx,NULL,zz}
unnest(anyarray) setof anyelement expand an array to a set of rows(将数组展开到一组行) unnest(ARRAY[1,2]) 1 2 (2 rows)

行转列

用postgresql的crosstab交叉函数

-- by zhengkai.blog.csdn.net
create table sales(year int, month int, qty int);
insert into sales values(2022, 1, 1000);
insert into sales values(2022, 2, 1500);
insert into sales values(2022, 7, 500);
insert into sales values(2022, 11, 1500);
insert into sales values(2022, 12, 2000);
insert into sales values(2023, 1, 1200);select * from crosstab('select year, month, qty from sales order by 1','select m from generate_series(1,12) m'
) as (year int,"Jan" int,"Feb" int,"Mar" int,"Apr" int,"May" int,"Jun" int,"Jul" int,"Aug" int,"Sep" int,"Oct" int,"Nov" int,"Dec" int
);year | Jan  | Feb  | Mar | Apr | May | Jun | Jul | Aug | Sep | Oct | Nov  | Dec
------+------+------+-----+-----+-----+-----+-----+-----+-----+-----+------+------2022 | 1000 | 1500 |     |     |     |     | 500 |     |     |     | 1500 | 20002023 | 1200 |      |     |     |     |     |     |     |     |     |      |
(2 rows)

可以参考pgsql官方的tablefunc实用说明

Function Returns Description
normal_rand(int numvals, float8 mean, float8 stddev) setof float8 Produces a set of normally distributed random values(产生一组正态分布的随机值)
crosstab(text sql) setof record Produces a “pivot table” containing row names plus N value columns, where N is determined by the row type specified in the calling query(生成一个包含行名和N个值列的“数据透视表”,其中N个由调用查询中指定的行类型决定)
crosstabN(text sql) setof table_crosstab_N Produces a “pivot table” containing row names plus N value columns. crosstab2, crosstab3, and crosstab4 are predefined, but you can create additional crosstabN functions as described below(生成一个包含行名和N个值列的“数据透视表”。交叉表2、交叉表3和交叉表4都是预定义的,但是您可以创建额外的跨表n函数,如下面所述)
crosstab(text source_sql, text category_sql) setof record Produces a “pivot table” with the value columns specified by a second query(生成具有由第二个查询指定的值列的“数据透视表”)
crosstab(text sql, int N) setof record Obsolete version of crosstab(text). The parameter N is now ignored, since the number of value columns is always determined by the calling query(过时版本的交叉表(文本)。参数N现在被忽略,因为值列的数量总是由调用查询决定)
connectby(text relname, text keyid_fld, text parent_keyid_fld [, text orderby_fld ], text start_with, int max_depth [, text branch_delim ]) setof record Produces a representation of a hierarchical tree structure(生成层次树结构的表示)

postgresql行转列、列转行相关推荐

  1. matlab 列转行,postgresql 行转列,列转行后加入到一个整体数据

    这里行转列的基本思想就是使用max,因为其他列下面都是NULL,所以可以Max最后就只能得到有值的这行 普通的查询: SELECT icd , case when (ROW_NUMBER() OVER ...

  2. SQL Server 行转列,列转行。多行转成一列

    一.多行转成一列(并以","隔开) 表名:A 表数据: 想要的查询结果: 查询语句: SELECT name ,value = ( STUFF(( SELECT ',' + val ...

  3. sqlserver中某列转成以逗号连接的字符串及逆转、数据行转列列转行

    Sql Server 列转逗号隔开的字符串 和 逆转 https://www.cnblogs.com/duanyuerui/p/7567692.html Sql server 中将数据行转列列转行(一 ...

  4. 行存、列存,堆表、AO表性能对比 - 阿里云HDB for PostgreSQL最佳实践

    标签 PostgreSQL , GIS , PostGIS , Greenplum , 空间检索 , GiST , B-Tree , geohash 背景 <Greenplum 行存.列存,堆表 ...

  5. Hive(行转列 列转行)

    Hive(行转列 列转行) 行转列 行专列常用的几种方式有 collect_list collect_set 举例说明 原数据 sql SELECT concat_ws("|",c ...

  6. 代码干货 | 行存、列存_堆表、AO表性能对比-阿里云HDB for PostgreSQL最佳实践

    本文来源于阿里云-云栖社区,原文点击这里. 标签 PostgreSQL , GIS , PostGIS , Greenplum , 空间检索 , GiST , B-Tree , geohash 背景 ...

  7. 【Excel】行转列+列转行

    行转列举例: 拷贝要转成列的行 选择一个空格,右键 选择性粘贴,并勾选 转置 结果如下: 多行转多列 以及多列转多行都是同样的操作

  8. greenplum 数据库如何增加列_Greenplum行存与列存的选择以及转换方法-阿里云开发者社区...

    背景 数据在数据库中的存储形式多种多样,比较常见的如 1. PostgreSQL的堆表,以行的形式存储,(当变成字段压缩后的长度超过数据块的四分之一时,会以TOAST的形式存储到TOAST表). 2. ...

  9. postgres默认安装后有哪些表_Greenplum 行存、列存,堆表、AO表的原理和选择

    行存和列存的原理 什么时候选择行存 什么时候选择列存 堆表和AO表的原理 什么时候选择堆表 什么时候选择AO表 测试对比行存deform和列存的性能差别 如何查看表的存储结构 Greenplum支持行 ...

  10. PostgreSQL中生成的列

    目录 介绍 背景 PostgreSQL 12 与SQL Server计算列比较 那么,生成的列与带有DEFAULT子句的常规列有何不同? 局限性 其他注意事项 PostgreSQL 11.x及更高版本 ...

最新文章

  1. Python 三十大实践、建议和技巧(附代码链接)
  2. linux id命令用来查看账户的uid和gid及所属分组及用户名
  3. Mac终端下打开sublime
  4. Gradle在Windows下的下载安装与配置以及在IDEA中配置以及修改jar包位置
  5. boost::timer模块检查在同一程序中使用 Chrono 和 Timer 不会导致链接错误
  6. springboot整合activemq加入会签,自动重发机制,持久化
  7. 独断专行站在互联网创业风口下的 00 后 CEO,没有钱该如何继续?
  8. Mysql插入中文的字段内容时乱码的解决方法
  9. C++ 顺序容器基础知识总结
  10. R 4.0 版本安装 rtools40教程,解决 Rtools is required to build R packages but is not currently installed 问题
  11. 浙江大学计算机考研最新,2017年浙江大学计算机考研复试分数线_浙江大学考研分数线...
  12. Qt的gui编程是,点击一次button出现两次action
  13. 有道云笔记markdown上传本地图片的方法
  14. JavaScript内存相关初了解:堆栈、引擎、闭包隐患
  15. 利用CAD提取高程点导出为TXT
  16. 为12306点赞!高铁动车买票正式上线选座功能
  17. android 绑定arp
  18. 三菱fx2n做从站的modbus通讯_三菱PLC控制变频器的方法
  19. 网易传媒数据指标体系建设实践
  20. 为什么六西格玛值得我们学习?-优思学院

热门文章

  1. 业务场景可以从哪方面考虑
  2. jQuery中获取兄弟元素的方法
  3. 阿里开源数据库连接池Druid一览
  4. 学历不高可以学java吗?有什么好的自学java方法
  5. 账户查询(结构体的应用)
  6. Python大数据教程:科学计算库NumPy案例之酒鬼漫步
  7. 服务器中了勒索病毒数据能找回吗?服务器中了勒索病毒怎么解决?
  8. 上海轨道交通展|2023上海国际先进轨道交通技术展【官网】
  9. 如何用MAC上的Safari检查iPhone手机App运行的Html页面
  10. python数据分析、数据挖掘,数据源从哪儿找?