广告
返回顶部
首页 > 资讯 > 数据库 >postgresql行转列与列转行图文教程
  • 731
分享到

postgresql行转列与列转行图文教程

postgresql行转列postgresql列转行 2023-06-11 10:06:39 731人浏览 泡泡鱼
摘要

目录列转行行转列总结列转行 postgresql列转行的思路主要是利用string_to_array进行数组转换,然后用unnest进行行拆分 select t.bid_unit,unit_id from u

列转行

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

postgresql行转列与列转行图文教程

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

postgresql行转列与列转行图文教程

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的解释

FunctionReturn TypeDescriptionExampleResult
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 anyelementexpand 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 | 2000
 2023 | 1200 |      |     |     |     |     |     |     |     |     |      |
(2 rows)

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

FunctionReturnsDescription
nORMal_rand(int numvals, float8 mean, float8 stddev)setof float8Produces a set of normally distributed random values(产生一组正态分布的随机值)
crosstab(text sql)setof recordProduces 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_NProduces 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 recordProduces a “pivot table” with the value columns specified by a second query(生成具有由第二个查询指定的值列的“数据透视表”)
crosstab(text sql, int N)setof recordObsolete 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 recordProduces a representation of a hierarchical tree structure(生成层次树结构的表示)

总结

到此这篇关于postgresql行转列与列转行的文章就介绍到这了,更多相关postgresql行转列 列转行内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

您可能感兴趣的文档:

--结束END--

本文标题: postgresql行转列与列转行图文教程

本文链接: https://www.lsjlt.com/news/261517.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作