KingbaseES 行列转换函数
关键字: 行专列,列转行, pivot, unpivot
行列转换是在数据分析中经常用到的一项功能,KingbaseES从V8R6C3B0071版本开始通过扩展插件(kdb_utils_function)支持了pivot和unpivot功能。在之前的版本如果需要进行行列转换操作要如何处理呢?下面介绍通用的写法,最后再介绍pivot和unpivot 用法。
一、行转列(pivot)
构造数据:
create table pivot_t1(month integer,fruitname text,quantity integer); insert into pivot_t1 values(1,'apple',1000);
insert into pivot_t1 values(2,'apple',2000);
insert into pivot_t1 values(3,'apple',3000);
insert into pivot_t1 values(4,'apple',4000);
insert into pivot_t1 values(1,'orange',1500);
insert into pivot_t1 values(2,'orange',2500);
insert into pivot_t1 values(3,'orange',3500);
insert into pivot_t1 values(4,'orange',4500);
insert into pivot_t1 values(1,'grape',1800);
insert into pivot_t1 values(2,'grape',2800);
insert into pivot_t1 values(3,'grape',3800);
insert into pivot_t1 values(4,'grape',4800);
insert into pivot_t1 values(1,'banana',1600);
insert into pivot_t1 values(2,'banana',2600);
insert into pivot_t1 values(3,'banana',3600);
insert into pivot_t1 values(4,'banana',4600);
1. case when语法
test=# select month,
test-# sum(case fruitname when 'apple' then quantity end) as apple,
test-# sum(case fruitname when 'orange' then quantity end) as orange,
test-# sum(case fruitname when 'grape' then quantity end) as grape,
test-# sum(case fruitname when 'banana' then quantity end) as banana
test-# from pivot_t1 group by month order by 1;
month | apple | orange | grape | banana
-------+-----------+------------+----------+--------
1 | 1000 | 1500 | 1800 | 1600
2 | 2000 | 2500 | 2800 | 2600
3 | 3000 | 3500 | 3800 | 3600
4 | 4000 | 4500 | 4800 | 4600
(4 rows)
2. CROSSTAB语法
crosstab() 函数由 tablefunc扩展包提供。
安装扩展 create extension tablefunc; test=# \df crosstab
函数列表
架构模式 | 名称 | 结果数据类型 | 参数数据类型 | 类型
----------+----------+--------------+---------------+------
public | crosstab | SETOF record | text | 函数
public | crosstab | SETOF record | text, integer | 函数
public | crosstab | SETOF record | text, text | 函数
函数说明:
crosstab ( sql text ) :生成一个“数据透视表”,其中包含行名称和 N 列值,其中 N 由调用查询中指定的行类型决定。
crosstab ( source_sql text, category_sql text ) :产生一个“数据透视表”,其值列由第二个查询指定。
crosstab ( sql text, N integer ) :crosstab(text)的废弃版本。
test=# SELECT *
test-# FROM crosstab(
test(# 'select month,fruitname,quantity
test'# from pivot_t1 order by 1,2','select distinct fruitname from pivot_t1 order by 1')
test-# AS (month int, apple varchar, banana varchar, grape varchar, orange varchar); month | apple | banana | grape | orange
-------+-------+--------+-------+--------
1 | 1000 | 1600 | 1800 | 1500
2 | 2000 | 2600 | 2800 | 2500
3 | 3000 | 3600 | 3800 | 3500
4 | 4000 | 4600 | 4800 | 4500
crosstab() 关键点:
第一个参数,带有按X,Y汇总的SQL子句,返回X,Y,Value格式的数据集;
第二个参数,SQL子句,返回用于水平表头中透视内容的所有值;
使用AS子句明确指定返回的每一个字段名称和类型,子句中列名需要与第二个参数order by结果一一对应。
3. pivot 语法:
test3=# select * from (select month,fruitname,quantity from pivot_t1)
test3-# pivot(sum(quantity) for fruitname in ('banana','apple' ,'orange','grape')); month | banana | apple | orange | grape
-------+--------+-------+--------+-------
1 | 1600 | 1000 | 1500 | 1800
2 | 2600 | 2000 | 2500 | 2800
3 | 3600 | 3000 | 3500 | 3800
4 | 4600 | 4000 | 4500 | 4800 (4 行记录)
pivot 计算指定的聚合值( sum(quantity) ),但是pivot 不包含显示的group by子句,pivot 隐式group by 是基于所有没在pivot子句中引用的列(month),以及在pivot in子句中指定的一组值。
二、列转行(unpivot)
构造数据:
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);
1.union all 语法
test=# select fruitname,'q1',q1 from unpivot_t1
test-# union all
test-# select fruitname,'q2',q2 from unpivot_t1
test-# union all
test-# select fruitname,'q3',q3 from unpivot_t1
test-# union all
test-# select fruitname,'q4',q4 from unpivot_t1; fruitname | ?COLUMN? | q1
-----------+----------+------
apple | q1 | 1100
orange | q1 | 2100
grape | q1 | 3100
banana | q1 | 4100
apple | q2 | 1200
orange | q2 | 2200
grape | q2 |
banana | q2 | 4200
apple | q3 | 1300
orange | q3 | 2300
grape | q3 | 3300
banana | q3 | 4300
apple | q4 | 1400
orange | q4 |
grape | q4 | 3400
banana | q4 | 4400 (16 rows)
2. unnest 函数
Unnest:将一个数组分解成一组行。 test=# select fruitname,unnest(array['q1','q2','q3','q4']),unnest(array[q1,q2,q3,q4]) from unpivot_t1
fruitname | unnest | unnest
-----------+--------+--------
apple | q1 | 1100
apple | q2 | 1200
apple | q3 | 1300
apple | q4 | 1400
orange | q1 | 2100
orange | q2 | 2200
orange | q3 | 2300
orange | q4 |
grape | q1 | 3100
grape | q2 |
grape | q3 | 3300
grape | q4 | 3400
banana | q1 | 4100
banana | q2 | 4200
banana | q3 | 4300
banana | q4 | 4400
3. unpivot 语法
test=# select fruitname,month,quantity from unpivot_t1 unpivot include nulls test-# (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 行记录)
参考文档:
[应用开发及迁移][服务器编程]SQL语言
KingbaseES 行列转换函数的更多相关文章
- oracle行列转换函数的使用
oracle 10g wmsys.wm_concat行列转换函数的使用: 首先让我们来看看这个神奇的函数wm_concat(列名),该函数可以把列值以","号分隔起来,并显示成一行 ...
- oracle 行列转换函数之WM_CONCAT和LISTAGG的使用(一)
一.wm_concat函数 wm_concat能够实现同样的功能,但是有时在11g中使用需要用to_char()进行转换,否则会出现不兼容现象(WMSYS.WM_CONCAT: 依赖WMSYS 用户, ...
- Oracle 行列转换函数pivot、unpivot的使用(二)
一.行转列pivot 关键函数pivot,其用法如下 pivot(聚合函数 for 列名 in(类型)) select * from table_name pivot(max(column_name) ...
- Oracle11g 行列转换函数PIVOT and UNPIVOT
作为Oracle开发工程师,推荐大伙看看 PIVOT and UNPIVOT Operators in Oracle Database 11g Release 1 This article shows ...
- Oracle行列转换
一.建表与插入数据 1.1.建表 create table kecheng ( id NUMBER, name ), course ), score NUMBER ); insert into kec ...
- Oracle学习之路-- 案例分析实现行列转换的几种方式
注:本文使用的数据库表为oracle自带scott用户下的emp,dept等表结构. 通过一个例子来说明行列转换: 需求:查询每个部门中各个职位的总工资 按我们最原始的思路可能会这么写: ...
- SQL Server中行列转换 Pivot UnPivot
SQL Server中行列转换 Pivot UnPivot PIVOT用于将列值旋转为列名(即行转列),在SQL Server 2000可以用聚合函数配合CASE语句实现 PIVOT的一般语法是:PI ...
- SQL(横表和纵表)行列转换,PIVOT与UNPIVOT的区别和使用方法举例,合并列的例子
使用过SQL Server 2000的人都知道,要想实现行列转换,必须综合利用聚合函数和动态SQL,具体实现起来需要一定的技巧,而在SQL Server 2005中,使用新引进的关键字PIVOT/UN ...
- SQL SERVER 合并重复行,行列转换
引用自:http://www.cnblogs.com/love-summer/archive/2012/03/27/2419778.html sql server2000 里面如何实现oracle10 ...
随机推荐
- babeljs源码
babel.min.js!function(e,t){"object"==typeof exports&&"object"==typeof mo ...
- 论文解读(ValidUtil)《Rethinking the Setting of Semi-supervised Learning on Graphs》
论文信息 论文标题:Rethinking the Setting of Semi-supervised Learning on Graphs论文作者:Ziang Li, Ming Ding, Weik ...
- 实战模拟│单点登录 SSO 的实现
目录 什么是单点登录 单点登录的凭证 父域 Cookie 方式 用户认证中心方式 localstorage方式 什么是单点登录 单点登录: SSO(Single Sign On) 用户只需登录一次,就 ...
- 练习-使用日期时间相关的API ,计算出一个人已经出生了多长时间
程序分析:(1)使用Scanner类获取出生日期(2)使用DataFormat类中的方法parse,把字符串的出生日期解析为Data格式的出生日期(3)把Data格式的出生日期转化为毫秒值(4)获取当 ...
- yum-config-manager: command not found
yum-config-manager: command not found ,这个是因为系统默认没有安装这个命令,这个命令在yum-utils 包里,可以通过命令yum -y install yum- ...
- vivo官网APP全机型UI适配方案
vivo 互联网客户端团队- Xu Jie 日益新增的机型,给开发人员带来了很多的适配工作.代码能不能统一.apk能不能统一.物料如何选取.样式怎么展示等等都是困扰开发人员的问题,本方案就是介绍不同机 ...
- Mysql 系列 | 日志模块
了解了 SQL 执行的流程,知道每一条语句都经过连接器.查询存储.分析器.优化器.执行器最后到存储引擎的过程.查询语句是如此,更新语句也不例外. 不同的是,更新语句会修改表数据,这里就涉及到两个重要的 ...
- Scala 练习题 学生分数案例
一.相关信息题目:1.统计班级人数2.统计学生的总分3.统计总分年级排名前十学生各科的分数4.统计总分大于年级平均分的学生5.统计每科都及格的学生6.统计偏科最严重的前100名学生数据样例(部分数据) ...
- 启用Hyper-v后,重启后界面提示 无法完成功能配置,正在撤销更改
安装docker后,提示需要启用hyper-v,在控制面板中勾选Hyper-v,然后重启,更新快完成就提示无法完成功能配置,正在撤销更改 解决方法 方法1 控制面板一个一个选 方法2 百度了n多内容, ...
- C# 基础知识-反射
一.反射 1>反射的命名空间是System.Reflection 2>是.Net框架提供的帮助类库,读取并使用matedata 二.反射基本用法 举例如下 1>Assembly as ...