http://www.postgresql.org/docs/9.3/static/tablefunc.html

安装extension tablefunc  :

digoal=# create extension tablefunc ; CREATE EXTENSION

几个主要函数介绍:

normal_rand:

normal_rand(int numvals, float8 mean, float8 stddev) returns setof float8
生成numvals个值,其平均为mean,标准偏差为stddev。
例如,两组数的集合 {0,5,9,14} 和 {5,6,8,9} 其平均值都是 7 ,但第二个集合具有较小的标准差。

digoal=# SELECT * FROM normal_rand(10, 5, 3); normal_rand --------------------- 2.40647424167461 3.97358357565708 6.83705458528592 3.55287920003402 -0.813572020831272 8.04323869492369 10.4944509678492 6.0051904741271 3.70728813314852 -0.0320731730703967 (10 rows) digoal=# SELECT * FROM normal_rand(10, 5, 3000); normal_rand ------------------- 865.360923345198 -1977.78887247386 2826.97836891189 1053.5080721384 366.541989988861 3071.38745724025 1832.10141151151 -1217.16007210777 -7018.42213577992 -1816.45691496064 (10 rows)

crosstab:

crosstab(text sql):

参数sql的格式必须固定如下:
select rowid, attribute, value from tb;
解释:
rowid:分类row行的约束,即组成group的条件。
attribute:分类column列的约束,即crosstab后形成的setof结果集中有刨去rowid这一列外,会分成多少组group(category)。
value:在分类万row和column后,对应row, column交叉地带应填充的值。
举例说明:
postgres=# CREATE TABLE ct(id SERIAL, rowid TEXT, attribute TEXT, value TEXT);
INSERT INTO ct(rowid, attribute, value) VALUES('test1','att1','val1');
INSERT INTO ct(rowid, attribute, value) VALUES('test1','att2','val2');
CREATE TABLE
postgres=# INSERT INTO ct(rowid, attribute, value) VALUES('test1','att1','val1');
INSERT 0 1
postgres=# INSERT INTO ct(rowid, attribute, value) VALUES('test1','att2','val2');
INSERT 0 1
postgres=# INSERT INTO ct(rowid, attribute, value) VALUES('test1','att3','val3');
INSERT 0 1
postgres=# INSERT INTO ct(rowid, attribute, value) VALUES('test1','att4','val4');
INSERT 0 1
postgres=# INSERT INTO ct(rowid, attribute, value) VALUES('test2','att1','val5');
INSERT 0 1
postgres=# INSERT INTO ct(rowid, attribute, value) VALUES('test2','att2','val6');
INSERT 0 1
postgres=# INSERT INTO ct(rowid, attribute, value) VALUES('test2','att3','val7');
INSERT 0 1
postgres=# INSERT INTO ct(rowid, attribute, value) VALUES('test2','att4','val8');
INSERT 0 1
postgres=# SELECT *
postgres-# FROM crosstab(
postgres(#   'select rowid, attribute, value
postgres'#    from ct
postgres'#    where attribute = ''att2'' or attribute = ''att3''
postgres'#    order by 1,2')
postgres-# AS ct(row_name text, category_1 text, category_2 text, category_3 text);row_name | category_1 | category_2 | category_3
----------+------------+------------+------------test1    | val2       | val3       | test2    | val6       | val7       |
(2 rows)

此外:结果集setof需要做"AS" 的说明罗列出气列数和列的类型。

crosstabN(text sql):

跟crosstab一样,只是不需要对结果集setof进行做“AS”声明,而是使用“N” 对齐进行说明结果集的列。
The tablefunc module includes crosstab2crosstab3, and crosstab4
tablefunc模块只支持N为2,3,4。
如果想之多更多则需要参考文档create type进行创建:http://www.postgresql.org/docs/9.3/static/tablefunc.html

crosstab(text source_sql, text category_sql):

解释:
source_sql:格式需规定为:select rowid, rowdt, attribute, value from tb;大体与crosstab一致。其中至少需要rowid, attribute, value着三个值。rowdt是额外可有可无的(但是需要注意"AS" 部分的定义要与其相符)。
category_sql:为分类列的约束group(category)。
postgres=# CREATE TABLE cth(rowid text, rowdt timestamp, attribute text, val text);
INSERT INTO cth VALUES('test1','01 March 2003','temperature','42');
CREATE TABLE
postgres=# INSERT INTO cth VALUES('test1','01 March 2003','temperature','42');
INSERT INTO cth VALUES('test1','01 March 2003','volts','2.6987');
INSERT 0 1
postgres=# INSERT INTO cth VALUES('test1','01 March 2003','test_result','PASS');
INSERT 0 1
postgres=# INSERT INTO cth VALUES('test1','01 March 2003','volts','2.6987');
INSERT 0 1
postgres=# INSERT INTO cth VALUES('test2','02 March 2003','temperature','53');
INSERT INTO cth VALUES('test2','02 March 2003','test_result','FAIL');
INSERT 0 1
postgres=# INSERT INTO cth VALUES('test2','02 March 2003','test_result','FAIL');
INSERT 0 1
postgres=# INSERT INTO cth VALUES('test2','02 March 2003','test_startdate','01 March 2003');
INSERT INTO cth VALUES('test2','02 March 2003','volts','3.1234');INSERT 0 1
postgres=# INSERT INTO cth VALUES('test2','02 March 2003','volts','3.1234');
INSERT 0 1
postgres=# SELECT * FROM crosstab
postgres-# (
postgres(#   'SELECT rowid, rowdt, attribute, val FROM cth ORDER BY 1',
postgres(#   'SELECT DISTINCT attribute FROM cth ORDER BY 1'
postgres(# )
postgres-# AS
postgres-# (
postgres(#        rowid text,
postgres(#        rowdt timestamp,
postgres(#        temperature int4,
postgres(#        test_result text,
postgres(#        test_startdate timestamp,
postgres(#        volts float8
postgres(# );rowid |        rowdt        | temperature | test_result |   test_startdate    | volts
-------+---------------------+-------------+-------------+---------------------+--------test1 | 2003-03-01 00:00:00 |          42 | PASS        |                     | 2.6987test2 | 2003-03-02 00:00:00 |          53 | FAIL        | 2003-03-01 00:00:00 | 3.1234
(2 rows)

注意:其中的value值是取最后一条:

postgres=# INSERT INTO cth VALUES('test1','02 March 2003','temperature','42');
INSERT 0 1
postgres=# INSERT INTO cth VALUES('test1','03 March 2003','temperature','42');
INSERT 0 1
postgres=# INSERT INTO cth VALUES('test1','04 March 2003','temperature','422');
INSERT 0 1

postgres=# select * from cth order by 1,3;
 rowid |        rowdt        |   attribute    |      val      
-------+---------------------+----------------+---------------
 test1 | 2003-03-01 00:00:00 | temperature    | 42
 test1 | 2003-03-02 00:00:00 | temperature    | 42
 test1 | 2003-03-03 00:00:00 | temperature    | 42
 test1 | 2003-03-04 00:00:00 | temperature    | 422
 test1 | 2003-03-01 00:00:00 | test_result    | PASS
 test1 | 2003-03-01 00:00:00 | volts          | 2.6987
 test2 | 2003-03-02 00:00:00 | temperature    | 53
 test2 | 2003-03-02 00:00:00 | test_result    | FAIL
 test2 | 2003-03-02 00:00:00 | test_startdate | 01 March 2003
 test2 | 2003-03-02 00:00:00 | volts          | 3.1234
(10 rows)

postgres=# SELECT * FROM crosstab
postgres-# (
postgres(#   'SELECT rowid, rowdt, attribute, val FROM cth ORDER BY 1',
postgres(#   'SELECT DISTINCT attribute FROM cth ORDER BY 1'
postgres(# )
postgres-# AS
postgres-# (
postgres(#        rowid text,
postgres(#        rowdt timestamp,
postgres(#        temperature int4,
postgres(#        test_result text,
postgres(#        test_startdate timestamp,
postgres(#        volts float8
postgres(# );rowid |        rowdt        | temperature | test_result |   test_startdate    | volts
-------+---------------------+-------------+-------------+---------------------+--------test1 | 2003-03-01 00:00:00 |         422 | PASS        |                     | 2.6987test2 | 2003-03-02 00:00:00 |          53 | FAIL        | 2003-03-01 00:00:00 | 3.1234
(2 rows)

转载于:https://www.cnblogs.com/xxvv/p/3766254.html

tablefunc 行转列相关推荐

  1. postgresql行转列、列转行

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

  2. 2021年大数据Hive(五):Hive的内置函数(数学、字符串、日期、条件、转换、行转列)

    全网最详细的Hive文章系列,强烈建议收藏加关注! 后面更新文章都会列出历史文章目录,帮助大家回顾知识重点. 目录 系列历史文章 前言 Hive的内置函数 一.数学函数 1. 取整函数: round ...

  3. MySQL 学习笔记(16)— 子查询(单行单列、一行多列、多行多列、 ALL、ANY、SOME 运算符、EXISTS 操作符)

    1. 子查询概念 子查询是指嵌套在其他语句(SELECT . INSERT . UPDATE . DELETE 等)中的 SELECT 语句:子查询也称为内查询( inner query )或者嵌套查 ...

  4. 【合并单元格】纵向合并单元格之前对数组处理【针对饿了么element的table的span-method合并行或列的计算方法】

    <template><el-table :span-method="spanMethod"><el-table-column label=" ...

  5. Algs4-1.1.13编写一段代码,打印出一个M行N列的二维数组的转置(交换行和列)

    1.1.13编写一段代码,打印出一个M行N列的二维数组的转置(交换行和列). public  class Test {     public static void main(String[] arg ...

  6. sqlserver 行转列

    还写了一篇Linq 实现 DataTable 行转列有时间大家可以看一下 sqlserver把行转成列在我们编码中是经常遇到的我做一个小例子大家看一下 1 --创建一个表 2 create table ...

  7. hive中array嵌套map以及行转列的使用

    1. 数据源信息 {"student": {"name":"king","age":11,"sex" ...

  8. SQL SERVER特殊行转列案列一则

    今天有个同事找我,他说他有个需求,需要进行行转列,但是又跟一般的行转列有些区别,具体需求如下所说,需要将表1的数据转换为表2的显示格式. 我想了一下,给出了一个解决方法,具体如下所示(先给出测试数据) ...

  9. hive 列转行_掌握这个SQL技巧超越80%的人——行转列/列转行

    在做特征工程的时候,会经常会碰到一个场景,比如手上有一张用户表user,记录了用户某款产品每一天各个功能的使用次数,存储方式类似key-value键值结构.具体如下: 用户使用行为统计表user 此时 ...

最新文章

  1. mysql 缓存区_Mysql缓存的配置和使用
  2. JVM调优总结 -Xms -Xmx -Xmn -Xss等
  3. springboot整合视图层Thymeleaf、freemarker
  4. php调用mysql库_PHP调用三种数据库的方法(1)
  5. 数据库事务转载基础一:oarcle事务
  6. 20145237 《信息安全系统设计基础》第2周学习总结
  7. mysql slave 线程 简书_【MySQL】你真的读懂了slave status吗?
  8. spark基础之基于yarn两种提交模式分析
  9. 超详细Gitlab Runner环境配置中文教程
  10. OC, OD门和线与逻辑
  11. PMP-PMBOK(第六版)--49个过程ITTO汇总
  12. 腾讯云服务器利用镜像部署WordPress个人网站
  13. 关于显示屏分辨率的问题
  14. 【Kubernetes】k8s使用stargz光速分发镜像
  15. [计算机网络]网络层
  16. Invalid argument: Subshape must have computed start >= end since stride is negative, but is 0 and 2
  17. 《Say As You Wish: Fine-grained Control of Image Caption Generation with Abstract Scene Graphs》阅读笔记
  18. 教你如何定位不合理的SQL?并优化之
  19. POI对Word操作参考
  20. Balsamiq 介绍

热门文章

  1. 青果灵动刘睿:3D页游将有更多机会
  2. IOS 9人机界面指南(1):UI设计基础
  3. sqlserver导出带数据的脚本文件
  4. 零基础学Python(第七章 while循环)
  5. oracle ORA-14452错误处理
  6. 字符集编码详解【ASCII 、GB2312、GBK、GB18030、unicode、UTF-8】(转)
  7. 强制关闭虚拟机后问题+解决
  8. SyncNavigator数据库同步软件8.4.1 中文版
  9. REST、RESTful 与 RESTful API
  10. 菜鸟级WEX5开发之路【用B/S开发经验应用到WEX5的开发中来】