列转行
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的更多相关文章

  1. postgresql 基本语法

    postgresql数据库创建/修改/删除等写入类代码语法总结: 1,创建库 2,创建/删除表 2.1 创建表 create table myTableName 2.2 如果表不存在则创建表 crea ...

  2. postgresql无法安装pldbgapi的问题

    要对函数进行调试需要安装插件pldbgapi,当初在windows上面的postgresql实例中执行了一下语句就安装上了: create extension pldbgapi; 但是在linux中执 ...

  3. ASP.NET MVC 使用 Petapoco 微型ORM框架+NpgSql驱动连接 PostgreSQL数据库

    前段时间在园子里看到了小蝶惊鸿 发布的有关绿色版的Linux.NET——“Jws.Mono”.由于我对.Net程序跑在Linux上非常感兴趣,自己也看了一些有关mono的资料,但是一直没有时间抽出时间 ...

  4. MongoDB与PostgresQL无责任初步测试

    PostgresQL一秒能插入多少条记录,MongoDB呢?读取的情况又如何?我写了一些简单的程序,得出了一些简单的数据,贴在这里分享,继续往下阅读前请注意下本文标题中的“无责任”,这表示此测试结果不 ...

  5. [PostgreSQL] 图解安装 PostgreSQL

    图解安装 PostgreSQL [博主]反骨仔 [原文地址]http://www.cnblogs.com/liqingwen/p/5894462.html 序 园友的一篇<Asp.Net Cor ...

  6. Asp.Net Core 项目实战之权限管理系统(3) 通过EntityFramework Core使用PostgreSQL

    0 Asp.Net Core 项目实战之权限管理系统(0) 无中生有 1 Asp.Net Core 项目实战之权限管理系统(1) 使用AdminLTE搭建前端 2 Asp.Net Core 项目实战之 ...

  7. PostgreSQL介绍以及如何开发框架中使用PostgreSQL数据库

    最近准备下PostgreSQL数据库开发的相关知识,本文把总结的PPT内容通过博客记录分享,本随笔的主要内容是介绍PostgreSQL数据库的基础信息,以及如何在我们的开发框架中使用PostgreSQ ...

  8. PostgreSql性能测试

    # PostgreSql性能测试 ## 1. 环境+ 版本:9.4.9+ 系统:OS X 10.11.5+ CPU:Core i5 2.7G+ 内存:16G+ 硬盘:256G SSD ## 2. 测试 ...

  9. postgresql 导出数据字典文档

    项目上需要整理目前数据库的数据字典文档.项目不规范,这种文档只要后期来补.这么多张表,每个字段都写到word文档里真心头大.就算前面写了个查询表结构的sql,但是最后整理到word里还是感觉有点麻烦. ...

  10. CentOS7下安装并简单设置PostgreSQL笔记

    为什么是PostgreSQL? 在.NET Core诞生之前,微软平台上最常见的开发组件便是.NET Framework + SQL Server了,但是现在.NET Core终于让跨平台部署成为了现 ...

随机推荐

  1. 如何使用git cherry-pick将同一个仓库的某个分支的某些commit合并到当前分支?

    答: git cherry-pick <another-branch's commit-id>

  2. Linux与linux之间传递文件、

    1.从linux本机文件上传到另一台linux格式:scp 要传的文件 root@目标ip:路径scp –r 要传的目录 root@目标ip:路径 例子: scp  /root/1.txt   roo ...

  3. 停止monkey的方法

    注意 Monkey启动后会不断地向被测对象发送随机事件流,直到事件执行完毕或者发生异常时才停止.在Monkey运行过程中,即便断开 与PC的连接,Monkey依然可以在手机上继续运行. 停止Monke ...

  4. 大数据(2)---HDFS集群搭建

    一.准备工作 1.准备几台机器,我这里使用VMware准备了四台机器,一个name node,三个data node. VMware安装虚拟机:https://www.cnblogs.com/niju ...

  5. jenkins容器内修改root密码--ubuntu系统

    http://www.voidcn.com/article/p-yvnoogkc-ng.html 由于jenkins官方镜像是ubuntu系统,所有啥的都用 sudo 换到root账号,然后登陆har ...

  6. Maven项目添加阿里云HBase依赖之后第一行才出现红叉报错“Missing artifact jdk.tools:jdk.tools:jar:1.6”的解决办法

    首先是从阿里云上下载了一个样例项目"hbase-demo",然后用eclipse打开,此时eclipse会去下载aliyun.hbase依赖. 等待一段时间后,pom.xml却报错 ...

  7. css解决fixed布局不会出现滚动条的问题

  8. iOS面试-关于性能优化

    目录 我要给出的建议将分为三个不同的等级: 入门级. 中级和进阶级: 入门级(这是些你一定会经常用在你app开发中的建议) 1. 用ARC管理内存2. 在正确的地方使用reuseIdentifier3 ...

  9. 敏捷管理的大概背景和Scrum的特性

    我们在转型Scrum敏捷开发时,一直会有一个很大的困惑问题,那就是我的团队适不适合使用敏捷项目管理工具和开发方式.我大概总结了下Scrum的大概样子可以参考下.总之敏捷管理不是口号,他只是一种工具,只 ...

  10. webdriervAPI基础元素定位

    from  selenium  import  webdriver driver  =  webdriver.Chorme() driver.get("http://www.baidu.co ...