KingbaseES 通过扩展插件支持了pivot 和unpivot 功能。以下以例子的方式介绍。

一、功能介绍

创建扩展:

create extension kdb_utils_function;

具体功能:

  • pivot(聚合函数 for 列名 in (类型)),其中 in ('') 中可以指定列名,还可以指定子查询
  • pivot(任一聚合函数 for 需转为列的值所在列名 in (需转为列名的值))
  • unpivot(新增值所在列的列名 for 新增列转为行后所在列的列名 in (需转为行的列名))

二、PIVOT 行转列

1、创建测试数据

create table pivot_t1(month integer,fruitname text,quantity integer, otherval integer);

insert into pivot_t1 values(1,'apple',1000,150);
insert into pivot_t1 values(2,'apple',2000,150);
insert into pivot_t1 values(3,'apple',3000,150);
insert into pivot_t1 values(4,'apple',4000,150);
insert into pivot_t1 values(1,'orange',1500,150);
insert into pivot_t1 values(2,'orange',2500,150);
insert into pivot_t1 values(3,'orange',3500,150);
insert into pivot_t1 values(4,'orange',4500,150);
insert into pivot_t1 values(1,'grape',1800,250);
insert into pivot_t1 values(2,'grape',2800,250);
insert into pivot_t1 values(3,'grape',3800,250);
insert into pivot_t1 values(4,'grape',4800,250);
insert into pivot_t1 values(1,'banana',1600,250);
insert into pivot_t1 values(2,'banana',2600,250);
insert into pivot_t1 values(3,'banana',3600,250);
insert into pivot_t1 values(4,'banana',4600,250);

2、例子

select * from (select month,fruitname,quantity from pivot_t1) pivot(sum(quantity) for fruitname in ('apple' as pingguo,'orange' as juzi,'grape' as putao));

 month | pingguo | juzi | putao
-------+---------+------+-------
1 | 1000 | 1500 | 1800
2 | 2000 | 2500 | 2800
3 | 3000 | 3500 | 3800
4 | 4000 | 4500 | 4800 test=# select * from (select month,fruitname,quantity from pivot_t1) pivot(sum(quantity) for fruitname in ('apple' ,'orange','grape'));
month | apple | orange | grape
-------+-------+--------+-------
1 | 1000 | 1500 | 1800
2 | 2000 | 2500 | 2800
3 | 3000 | 3500 | 3800
4 | 4000 | 4500 | 4800
(4 rows) test=# select * from pivot_t1 pivot(sum(quantity) for fruitname in ('apple' ,'orange','grape'));
month | otherval | apple | orange | grape
-------+----------+-------+--------+-------
1 | 150 | 1000 | 1500 |
1 | 250 | | | 1800
2 | 150 | 2000 | 2500 |
2 | 250 | | | 2800
3 | 150 | 3000 | 3500 |
3 | 250 | | | 3800
4 | 150 | 4000 | 4500 |
4 | 250 | | | 4800
(8 rows)

pivot 计算指定的聚合值( sum(quantity) ),但是pivot 不包含显示的group by子句,pivot 隐式group by 是基于所有没在pivot子句中引用的列(month),以及在pivot in子句中指定的一组值。

三、UNPIVOT 列转行

1、创建测试数据

create table unpivot_t1(fruitname text,q1 integer,q2 integer,q3 integer,q4 integer);
insert into unpivot_t1 values('apple', 1100,1200,1300,1400);
insert into unpivot_t1 values('orange',2100,2200,2300,null);
insert into unpivot_t1 values('grape', 3100,null,3300,3400);
insert into unpivot_t1 values('banana',4100,4200,4300,4400);

2、测试结果

select fruitname,month,quantity from unpivot_t1 unpivot include nulls (quantity for month in (q1 as 'Q1',q2 as 'Q2',q3 as 'Q3',q4 as 'Q4')) order by fruitname,month;

 fruitname | month | quantity
-----------+-------+----------
apple | Q1 | 1100
apple | Q2 | 1200
apple | Q3 | 1300
apple | Q4 | 1400
banana | Q1 | 4100
banana | Q2 | 4200
banana | Q3 | 4300
banana | Q4 | 4400
grape | Q1 | 3100
grape | Q2 |
grape | Q3 | 3300
grape | Q4 | 3400
orange | Q1 | 2100
orange | Q2 | 2200
orange | Q3 | 2300
orange | Q4 |
(16 rows)

四、crosstab 行转列

create extension tablefunc;

test=# select * from crosstab('select month,fruitname,quantity from pivot_t1 order by month',$$values('apple'),('orange'),('grape')$$) as sg(month int,pingguo int,juzi int,putao int);
month | pingguo | juzi | putao
-------+---------+------+-------
1 | 1000 | 1500 | 1800
2 | 2000 | 2500 | 2800
3 | 3000 | 3500 | 3800
4 | 4000 | 4500 | 4800
(4 rows) test=# select * from crosstab('select month,fruitname,quantity from pivot_t1 order by month') as sg(month int,"apple" int,"orange" int,"grape" int,"banana" int);
month | apple | orange | grape | banana
-------+-------+--------+-------+--------
1 | 1000 | 1500 | 1800 | 1600
2 | 2500 | 2800 | 2600 | 2000
3 | 3800 | 3500 | 3600 | 3000
4 | 4600 | 4500 | 4000 | 4800
(4 rows)

  

注意:对于crosstab,order by 非常关键,

KingbaseES 支持pivot and unpivot 功能的更多相关文章

  1. (1.3)DML增强功能-Apply、pivot、unpivot、for xml path行列转换

    深入了解行列转换请参考另一篇文章:https://www.cnblogs.com/gered/p/9271581.html 总结: 1.apply一般形式 --基本形式 SELECT a FROM d ...

  2. SQL Server 行列相互转换命令:PIVOT和UNPIVOT使用详解

    一.使用PIVOT和UNPIVOT命令的SQL Server版本要求 1.数据库的最低版本要求为SQL Server 2005 或更高. 2.必须将数据库的兼容级别设置为90 或更高. 3.查看我的数 ...

  3. PIVOT 和 UNPIVOT 命令的SQL Server版本

    I:使用 PIVOT 和 UNPIVOT 命令的SQL Server版本要求 1.数据库的最低版本要求为 SQL Server 2005 或 更高 2.必须将数据库的兼容级别设置为 90 或 更高 3 ...

  4. T-SQL行列相互转换命令:PIVOT和UNPIVOT使用详解

    最近在维护一个ERP 做二次开发 ,在查找数据源的时候看到前辈写的SQL ,自己能力有限 ,就在网上找找有关这几个关键字的使用方法.做出随笔以做学习之用 T-SQL语句中,PIVOT命令可以实现数据表 ...

  5. Pivot 和 Unpivot

    在TSQL中,使用Pivot和Unpivot运算符将一个关系表转换成另外一个关系表,两个命令实现的操作是“相反”的,但是,pivot之后,不能通过unpivot将数据还原.这两个运算符的操作数比较复杂 ...

  6. pivot 与 unpivot 函数是SQL05新提供的2个函数

    pivot 与 unpivot 函数是SQL05新提供的2个函数   ----------------------------------------------------------------- ...

  7. PIVOT和UNPIVOT使用详解

    一.使用PIVOT实现数据表的列转行 建表语句: DROP TABLE STUDENT; CREATE TABLE STUDENT ( 学生编号 BYTE) NULL , 姓名 BYTE) NULL ...

  8. [转]Oracle SQL函数pivot、unpivot转置函数实现行转列、列转行

    原文地址:http://blog.csdn.net/seandba/article/details/72730657 函数PIVOT.UNPIVOT转置函数实现行转列.列转行,效果如下图所示: 1.P ...

  9. T-SQL Part IX, PIVOT and UNPIVOT

    不同于CROSS JOIN, CROSS APPLY, OUTER APPLY,MSDN文档对PIVOT和UNPIVOT 想得重视了一点,单独做了一个页面来介绍. 简单来说,PIVOT用来把行转成列, ...

随机推荐

  1. 一分钟入门 Babel(下一代 JavaScript 语法的编译器)

    简单来说把 JavaScript 中 es2015/2016/2017/2046 的新语法转化为 es5,让低端运行环境(如浏览器和 node )能够认识并执行.严格来说,babel 也可以转化为更低 ...

  2. mysql InnoDB通过.frm和.ibd恢复表和数据

    ibdata1是一个用来构建innodb系统表空间的文件,这个文件包含了innodb表的元数据.撤销记录.修改buffer和双写buffer.如果file-per-table选项打开的话,该文件则不一 ...

  3. halcon数组的一些使用

    没啥好讲的,这里对于不是数组部分的东西就不进行讲解了. area_center(RegionOpening,Area, Row, Column).使用area_center来求区域的中心和面积时,返回 ...

  4. PTA(BasicLevel)-1014 福尔摩斯的约会

    一.问题描述 大侦探福尔摩斯接到一张奇怪的字条:我们约会吧! 3485djDkxh4hhGE 2984akDfkkkkggEdsb s&hgsfdk d&Hyscvnm.大侦探很快就明 ...

  5. ReentrantLock 公平锁源码 第1篇

    ReentrantLock 1 这篇还是接着ReentrantLock的公平锁,没看过第0篇的可以先去看上一篇https://www.cnblogs.com/sunankang/p/16456342. ...

  6. Android Studio的初次认识

    Android的初试 一.认识Android Studio 在我们新建项目的时候,会遇到这样的一个窗口,首先我们认识一下这些都是什么,这样我们才能够更好的进行下一步的学习! 这里的 Phone and ...

  7. intellidea 快捷键-*01

    快捷键: 0.竖向选择文本: alt+shift+insert:https://www.cnblogs.com/JonaLin/p/11422110.html 如果想修改快捷键(setting-> ...

  8. 千万不要把Request传递到异步线程里面!有坑!

    你好哇,我是歪歪. 前几天在网上冲浪的时候看到一篇技术文章,讲的是他把一个 request 请求传递到了线程池里面,然后遇到了一个匪夷所思的情况. 他写了这篇文章,把自己针对这个问题的探索过程分享了出 ...

  9. 86开关、家电、台扇等6键6路6感应通道高抗干扰触摸IC-VK3606D,稳定性好,抗干扰能力强

    概述: VK3606D SOP16具有6个触摸按键,可用来检测外部触摸按键上人手的触摸动作.该芯片具有较高的集成度,仅需极少的外部组件便可实现触摸按键的检测.提供了6路1对1直接输出低电平有效.最长输 ...

  10. 建立二叉树的二叉链表(严6.65)--------西工大noj

    需要注意的点:在创建二叉树的函数中,如果len1==len2==0,一定要把(*T)置为NULL然后退出循环 #include <stdio.h> #include <stdlib. ...