PostgreSQL练习2
列转行
CREATE TABLE sdb.t_col_row
(id int,
c1 varchar(10),
c2 varchar(10),
c3 varchar(10)
)
INSERT INTO sdb.t_col_row( id, c1, c2, c3)VALUES (1, 'v11', 'v21', 'v31');
INSERT INTO sdb.t_col_row( id, c1, c2, c3)VALUES (2, 'v12', 'v22', null);
INSERT INTO sdb.t_col_row( id, c1, c2, c3)VALUES (3, 'v13', null, 'v33');
INSERT INTO sdb.t_col_row( id, c1, c2, c3)VALUES (4, null, 'v24', 'v34');
INSERT INTO sdb.t_col_row( id, c1, c2, c3)VALUES (5, 'v11', null, null);
INSERT INTO sdb.t_col_row( id, c1, c2, c3)VALUES (6, null, null, 'v35');
INSERT INTO sdb.t_col_row( id, c1, c2, c3)VALUES (7, null, null, null);
commit;
select * from sdb.t_col_row;
select id,'c1' cn,c1 from sdb.t_col_row;
select id,'c2' cn,c2 from sdb.t_col_row;
select id,'c3' cn,c3 from sdb.t_col_row;
行转列
CREATE TABLE sdb.t_row_col
as
select id,'c1' cn,c1 cv
from sdb.t_col_row
union all
select id,'c2' cn,c2 cv
from sdb.t_col_row
union all
select id,'c3' cn,c3 cv
from sdb.t_col_row
select * from sdb.t_row_col
order by 1,2;
select id,
max(case when cn='c1' then cv else null end)as c1,
max(case when cn='c2' then cv else null end)as c2,
max(case when cn='c3' then cv else null end)as c3
from sdb.t_row_col
group by id
order by 1;
多行转成字符串
CREATE TABLE sdb.t_col_str
as
select * from sdb.t_col_row;
select * from sdb.t_col_str;
select id,c1||','||c2||','||c3 as c123
from sdb.t_col_str;
CREATE TABLE sdb.t_row_str
(
id int,col varchar(10)
);
INSERT INTO sdb.t_row_str( id, col)VALUES (1, 'a');
INSERT INTO sdb.t_row_str( id, col) VALUES (1, 'b');
INSERT INTO sdb.t_row_str(id, col)VALUES (1, 'c');
INSERT INTO sdb.t_row_str( id, col) VALUES (2, 'a');
INSERT INTO sdb.t_row_str( id, col) VALUES (2, 'd');
INSERT INTO sdb.t_row_str( id, col)VALUES (2, 'e');
INSERT INTO sdb.t_row_str( id, col) VALUES (3, 'c');
commit;
select * from sdb.t_row_str;
select id,
max(case when rn=1 then col else null end)||
max(case when rn=2 then','||col else null end)||
max(case when rn=3 then','||col else null end) str
from (
select id,col,row_NUMBER() over(partition by id order by col) as rn
from sdb.t_row_str
)t
group by id
order by 1;
字符串转成多列
CREATE TABLE sdb.t_str_col
as select id,c1||','||c2||','||c3 as c123
from sdb.t_col_str;
select * from sdb.t_str_col;
select c123,split_part(c123,',',1),split_part(c123,',',2),split_part(c123,',',3)
from sdb.t_str_col;
字符串转成多行
CREATE TABLE sdb.t_str_row
as
select id,
max(case when rn=1 then col else null end)||
max(case when rn=2 then ','||col else null end)||
max(case when rn=3 then ','||col else null end) str
from (select id,
col,row_number() over(partition by id order by col) as rn
from sdb.t_row_str) t
group by id
order by 1;
select * from sdb.t_str_row;
select id,1 as p,split_part(str,',',1) as cv
from sdb.t_str_row
union all
select id,1 as p,split_part(str,',',2) as cv
from sdb.t_str_row
union all
select id,1 as p,split_part(str,',',3) as cv
from sdb.t_str_row
order by 1,2;
coalesce()函数:将空值替换成其他值,返回第一个非空值
coalesce(name,'');
窗口函数
例子1:
DROP TABLE IF EXISTS empsalary;
CREATE TABLE empsalary(
depname varchar,
empno bigint,
salary int,
enroll_date date
);
INSERT INTO empsalary VALUES('develop',10, 5200, '2007/08/01');
INSERT INTO empsalary VALUES('sales', 1, 5000, '2006/10/01');
INSERT INTO empsalary VALUES('personnel', 5, 3500, '2007/12/10');
INSERT INTO empsalary VALUES('sales', 4, 4800, '2007/08/08');
INSERT INTO empsalary VALUES('sales', 6, 5500, '2007/01/02');
INSERT INTO empsalary VALUES('personnel', 2, 3900, '2006/12/23');
INSERT INTO empsalary VALUES('develop', 7, 4200, '2008/01/01');
INSERT INTO empsalary VALUES('develop', 9, 4500, '2008/01/01');
INSERT INTO empsalary VALUES('sales', 3, 4800, '2007/08/01');
INSERT INTO empsalary VALUES('develop', 8, 6000, '2006/10/01');
INSERT INTO empsalary VALUES('develop', 11, 5200, '2007/08/15');
select * from sdb.empsalary;
--统计人员所在部门的总薪水,平均薪水和部门的详细情况
select sum(salary) over(partition by empsalary.depname),avg(salary) over(partition by empsalary.depname),*
from sdb.empsalary;
--统计人员所在部门的薪水排名情况
select rank() over(partition by empsalary.depname order by salary),*
from sdb.empsalary;
--统计人员所在部门薪水排名,薪水级别,部门名称
select sum(salary) over(partition by empsalary.depname order by salary),salary,empsalary.depname
from sdb.empsalary;
例子2:
DROP TABLE IF EXISTS sdb.products;
CREATE TABLE sdb.products (
"id" varchar(10) COLLATE "default",
"name" text COLLATE "default",
"price" numeric,
"uid" varchar(14) COLLATE "default",
"type" varchar(100) COLLATE "default"
)
WITH (OIDS=FALSE);
BEGIN;
INSERT INTO sdb.products VALUES ('0006', 'iPhone X', '9600', null, '电器');
INSERT INTO sdb.products VALUES ('0012', '电视', '3299', '4', '电器');
INSERT INTO sdb.products VALUES ('0004', '辣条', '5.6', '4', '零食');
INSERT INTO sdb.products VALUES ('0007', '薯条', '7.5', '1', '零食');
INSERT INTO sdb.products VALUES ('0009', '方便面', '3.5', '1', '零食');
INSERT INTO sdb.products VALUES ('0005', '铅笔', '7', '4', '文具');
INSERT INTO sdb.products VALUES ('0014', '作业本', '1', null, '文具');
INSERT INTO sdb.products VALUES ('0001', '鞋子', '27', '2', '衣物');
INSERT INTO sdb.products VALUES ('0002', '外套', '110.9', '3', '衣物');
INSERT INTO sdb.products VALUES ('0013', '围巾', '93', '5', '衣物');
INSERT INTO sdb.products VALUES ('0008', '香皂', '17.5', '2', '日用品');
INSERT INTO sdb.products VALUES ('0010', '水杯', '27', '3', '日用品');
INSERT INTO sdb.products VALUES ('0015', '洗发露', '36', '1', '日用品');
INSERT INTO sdb.products VALUES ('0011', '毛巾', '15', '1', '日用品');
INSERT INTO sdb.products VALUES ('0003', '手表', '1237.55', '5', '电器');
INSERT INTO sdb.products VALUES ('0016', '绘图笔', '15', null, '文具');
INSERT INTO sdb.products VALUES ('0017', '汽水', '3.5', null, '零食');
COMMIT;
select*from sdb.products ;
--按price列升序
select type,name,price,row_number() over(order by price asc)as idx
from sdb.products;
--在类别内按照价格升序排列
select type,name,price,row_number() over(partition by type order by price asc)as idx
from sdb.products;
--分类排序序号并列 rank()
select type,name,price,rank() over(partition by type order by price asc)
from sdb.products;
--限制序号0-1之间 percernt_rank()
select type,name,price,percent_rank() over(partition by type order by price asc)
from sdb.products;
--限制序号在0-1之间相对排名
select type,name,price,cume_dist() over(partition by type order by price asc)
from sdb.products;
--限制最大序号为制定数字序号 ntile()
select type,name,price,ntile(2) over(partition by type order by price asc)
from sdb.products;
--获取分类子项排序中的第一条记录的某个字段的值
select type,name,price,first_value(name) over(partition by type order by price asc)
from sdb.products;
--窗口函数+聚合函数
select id,type,name,price,
sum(price) over w1 类别金额合计,
(sum(price) over (order by type))/sum(price) over() 类别总额占所有品类商品百分比,
rank() over w3 排名,
sum(price) over() 金额总计
from sdb.products
window
w1 as (partition by type),
w2 as (partition by type rows between unbounded preceding and unbounded following),
w3 as (partition by type order by price desc)
order by type,price asc;
PostgreSQL练习2的更多相关文章
- postgresql 基本语法
postgresql数据库创建/修改/删除等写入类代码语法总结: 1,创建库 2,创建/删除表 2.1 创建表 create table myTableName 2.2 如果表不存在则创建表 crea ...
- postgresql无法安装pldbgapi的问题
要对函数进行调试需要安装插件pldbgapi,当初在windows上面的postgresql实例中执行了一下语句就安装上了: create extension pldbgapi; 但是在linux中执 ...
- ASP.NET MVC 使用 Petapoco 微型ORM框架+NpgSql驱动连接 PostgreSQL数据库
前段时间在园子里看到了小蝶惊鸿 发布的有关绿色版的Linux.NET——“Jws.Mono”.由于我对.Net程序跑在Linux上非常感兴趣,自己也看了一些有关mono的资料,但是一直没有时间抽出时间 ...
- MongoDB与PostgresQL无责任初步测试
PostgresQL一秒能插入多少条记录,MongoDB呢?读取的情况又如何?我写了一些简单的程序,得出了一些简单的数据,贴在这里分享,继续往下阅读前请注意下本文标题中的“无责任”,这表示此测试结果不 ...
- [PostgreSQL] 图解安装 PostgreSQL
图解安装 PostgreSQL [博主]反骨仔 [原文地址]http://www.cnblogs.com/liqingwen/p/5894462.html 序 园友的一篇<Asp.Net Cor ...
- Asp.Net Core 项目实战之权限管理系统(3) 通过EntityFramework Core使用PostgreSQL
0 Asp.Net Core 项目实战之权限管理系统(0) 无中生有 1 Asp.Net Core 项目实战之权限管理系统(1) 使用AdminLTE搭建前端 2 Asp.Net Core 项目实战之 ...
- PostgreSQL介绍以及如何开发框架中使用PostgreSQL数据库
最近准备下PostgreSQL数据库开发的相关知识,本文把总结的PPT内容通过博客记录分享,本随笔的主要内容是介绍PostgreSQL数据库的基础信息,以及如何在我们的开发框架中使用PostgreSQ ...
- PostgreSql性能测试
# PostgreSql性能测试 ## 1. 环境+ 版本:9.4.9+ 系统:OS X 10.11.5+ CPU:Core i5 2.7G+ 内存:16G+ 硬盘:256G SSD ## 2. 测试 ...
- postgresql 导出数据字典文档
项目上需要整理目前数据库的数据字典文档.项目不规范,这种文档只要后期来补.这么多张表,每个字段都写到word文档里真心头大.就算前面写了个查询表结构的sql,但是最后整理到word里还是感觉有点麻烦. ...
- CentOS7下安装并简单设置PostgreSQL笔记
为什么是PostgreSQL? 在.NET Core诞生之前,微软平台上最常见的开发组件便是.NET Framework + SQL Server了,但是现在.NET Core终于让跨平台部署成为了现 ...
随机推荐
- sed中使用变量及变量中存在特殊字符‘/’处理
sed中使用变量,普通的处理方式无法解析变量 如当前file文件中存在字符串pedis,现将其替换为redis [root@localhost work]# cat file pedis 如下两种替换 ...
- jmeter-移动端接口测试中遇到的问题,http与https
解决:将请求默认值的http改成https
- ILI9341液晶显示
17.1液晶显示原理 TFT-LCD(Thin Film Transistor Liquid Crystal Display)即薄膜晶体管液晶显示器,是微电子技术与液晶显示器技术巧妙结合的的一种技术. ...
- mongodb MongoDB C#/.NET driver version
The first column lists the driver version(s). C#/.NET Driver Version MongoDB 2.6 MongoDB 3.0 MongoDB ...
- MSE, MAE, Huber loss详解
转载:https://mp.weixin.qq.com/s/Xbi5iOh3xoBIK5kVmqbKYA https://baijiahao.baidu.com/s?id=16119517755261 ...
- python3 高级编程(三) 使用@property
@property装饰器就是负责把一个方法变成属性调用的. @property广泛应用在类的定义中,可以让调用者写出简短的代码,同时保证对参数进行必要的检查,这样,程序运行时就减少了出错的可能性 cl ...
- 工具的更新换代 总是要折腾一下Windows10下Java Web 开发环境的配置
Windows10下Java Web 开发环境的配置 由于经常性遗忘,所以整理一下 Java Web 开发环境的搭建与配置,利人利己 主要分为以下几步,需要可以挑着看 Windows下 JDK 的下载 ...
- React Native运行安卓报错解决记录
1>Error:Configuration with name ‘default’ not found. 解决链接: http://blog.csdn.net/u011240877/articl ...
- Java连接阿里云HBase示例
使用前要在阿里云的 HBase控制台中点击"修改网络白名单",然后将你的ip地址(会有提示的)添加到网络白名单中,这样以后才能访问. 所需依赖: <dependencies& ...
- 敏捷管理的大概背景和Scrum的特性
我们在转型Scrum敏捷开发时,一直会有一个很大的困惑问题,那就是我的团队适不适合使用敏捷项目管理工具和开发方式.我大概总结了下Scrum的大概样子可以参考下.总之敏捷管理不是口号,他只是一种工具,只 ...