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终于让跨平台部署成为了现 ...
随机推荐
- mac安装需要的骚操作
显示隐藏文件 defaults write com.apple.finder AppleShowAllFiles -bool true; KillAll Finder 允许任何来源 sudo spct ...
- Spring注解驱动——组件注册系列
1.@Configuration 从Spring3.0,@Configuration用于定义配置类,可替换xml配置文件,被注解的类内部包含有一个或多个被@Bean注解的方法,这些方法将会被Annot ...
- MySQL数据分析(16)— 数据操作之增删改查
前面我们说学习MySQL要从三个层面,四大逻辑来学,三个层面就是库层面,表层面和数据层面对吧,数据库里放数据表,表里放数据是吧,大家可以回忆PPT中jacky的这图,我们已经学完了库层面和表层面,从本 ...
- springboot之搭建第一个helloworld程序
1.下载基本框架 在网站:https://start.spring.io/ 全部默认,基本没有改动 选择依赖,当然也可以自己在pom.xml加,我们直接在这里选择. 只选择Spring Web Sta ...
- Js中Array常用方法小结
说起Array的方法,不免让人皱一下眉头,下面我们从增删改查角度依次来总结. 1.增 push: 将传入的参数 ,插入数组的尾部,并返回新数组的长度.不管传入参数为一个值还是一个数组,都作为插入数组的 ...
- 入门display:inline-block运用
这是我第一篇博客,是我新的开始,我要用博客记录我的学习之旅,在这里我要感谢我的哥哥,他带我开阔了眼界,纠正了我的格局,给我带来了正能量.我是一个小白,学习的路还很长很长,学习了10天HTML与css, ...
- JS函数传递字符串参数(符号转义)
原文链接:https://blog.csdn.net/Myname_China/article/details/82907965 JS函数传递字符串参数,如果没有转义处理,在接收的时候无法正确的接收字 ...
- 第04组 Beta冲刺(1)
队名:斗地组 组长博客:地址 作业博客:Beta冲刺(1/4) 各组员情况 林涛(组长) 过去两天完成了哪些任务: 1.分配展示任务 2.收集各个组员的进度 3.写博客 展示GitHub当日代码/文档 ...
- arcgis python 标注
import arcpy mxd = arcpy.mapping.MapDocument("current") lyr = arcpy.mapping.ListLayers(mxd ...
- python 获取昨天的日期
from datetime import timedelta, datetime yesterday = datetime.today()+timedelta(-1) yesterday_format ...