tablefunc 行转列
安装extension tablefunc :
digoal=# create extension tablefunc ; CREATE EXTENSION
几个主要函数介绍:
normal_rand:
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):
举例说明:
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)
crosstabN(text sql):
crosstab2
, crosstab3
, and crosstab4
crosstab(text source_sql, text category_sql):
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 行转列相关推荐
- postgresql行转列、列转行
列转行 postgresql列转行的思路主要是利用string_to_array进行数组转换,然后用unnest进行行拆分 select t.bid_unit,unit_id from unit t ...
- 2021年大数据Hive(五):Hive的内置函数(数学、字符串、日期、条件、转换、行转列)
全网最详细的Hive文章系列,强烈建议收藏加关注! 后面更新文章都会列出历史文章目录,帮助大家回顾知识重点. 目录 系列历史文章 前言 Hive的内置函数 一.数学函数 1. 取整函数: round ...
- MySQL 学习笔记(16)— 子查询(单行单列、一行多列、多行多列、 ALL、ANY、SOME 运算符、EXISTS 操作符)
1. 子查询概念 子查询是指嵌套在其他语句(SELECT . INSERT . UPDATE . DELETE 等)中的 SELECT 语句:子查询也称为内查询( inner query )或者嵌套查 ...
- 【合并单元格】纵向合并单元格之前对数组处理【针对饿了么element的table的span-method合并行或列的计算方法】
<template><el-table :span-method="spanMethod"><el-table-column label=" ...
- Algs4-1.1.13编写一段代码,打印出一个M行N列的二维数组的转置(交换行和列)
1.1.13编写一段代码,打印出一个M行N列的二维数组的转置(交换行和列). public class Test { public static void main(String[] arg ...
- sqlserver 行转列
还写了一篇Linq 实现 DataTable 行转列有时间大家可以看一下 sqlserver把行转成列在我们编码中是经常遇到的我做一个小例子大家看一下 1 --创建一个表 2 create table ...
- hive中array嵌套map以及行转列的使用
1. 数据源信息 {"student": {"name":"king","age":11,"sex" ...
- SQL SERVER特殊行转列案列一则
今天有个同事找我,他说他有个需求,需要进行行转列,但是又跟一般的行转列有些区别,具体需求如下所说,需要将表1的数据转换为表2的显示格式. 我想了一下,给出了一个解决方法,具体如下所示(先给出测试数据) ...
- hive 列转行_掌握这个SQL技巧超越80%的人——行转列/列转行
在做特征工程的时候,会经常会碰到一个场景,比如手上有一张用户表user,记录了用户某款产品每一天各个功能的使用次数,存储方式类似key-value键值结构.具体如下: 用户使用行为统计表user 此时 ...
最新文章
- mysql 缓存区_Mysql缓存的配置和使用
- JVM调优总结 -Xms -Xmx -Xmn -Xss等
- springboot整合视图层Thymeleaf、freemarker
- php调用mysql库_PHP调用三种数据库的方法(1)
- 数据库事务转载基础一:oarcle事务
- 20145237 《信息安全系统设计基础》第2周学习总结
- mysql slave 线程 简书_【MySQL】你真的读懂了slave status吗?
- spark基础之基于yarn两种提交模式分析
- 超详细Gitlab Runner环境配置中文教程
- OC, OD门和线与逻辑
- PMP-PMBOK(第六版)--49个过程ITTO汇总
- 腾讯云服务器利用镜像部署WordPress个人网站
- 关于显示屏分辨率的问题
- 【Kubernetes】k8s使用stargz光速分发镜像
- [计算机网络]网络层
- Invalid argument: Subshape must have computed start >= end since stride is negative, but is 0 and 2
- 《Say As You Wish: Fine-grained Control of Image Caption Generation with Abstract Scene Graphs》阅读笔记
- 教你如何定位不合理的SQL?并优化之
- POI对Word操作参考
- Balsamiq 介绍