列转行
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. 使用Ajax向SpringMVC传递Json数据

    这篇文章已经过时了. 请参考比较合适的前后端交互方式. 1.保证SpringMVC配置成功了. 2.在pom.xml中追加Jackson相关的依赖 <dependency> <gro ...

  2. 8月清北学堂培训 Day5

    今天是杨思祺老师的讲授~ 最短路练习题: POJ 1125 Stockbroker Grapevine 有 N 个股票经济人可以互相传递消息,他们之间存在一些单向的通信路径.现在有一个消息要由某个人开 ...

  3. 1558:聚会 ybt

    1558:聚会 ybt 题解(看似很难,其实要是摸清了实质这就是个大水题) 上题目 1558:聚会 时间限制: 1000 ms         内存限制: 524288 KB提交数: 82     通 ...

  4. 二十五、grub (Boot Loader) 以及修复grub

    双系统安装(先Windows后Linux,以免windows NTloader会覆盖Linux loader) GRUB Grand Uniform Bootloader CentOS5,6 grub ...

  5. yum install 报错

    把python2升级到python3以后,yum报错: [root@localhost Python-]# yum install openssl File except KeyboardInterr ...

  6. sh: 1: Syntax error: Bad fd number

    Start on Ubuntu 6.10,Using dash default(theDebian Almquist Shell) instead bash(the GNUBourne-Again S ...

  7. qt 创建及调用QT的 DLL

    先讲一下对QT动态链接库的调用方法,主要包括: 1.显式链接DLL,调用DLL的全局函数,采用Qt的QLibrary方法 2.显示链接DLL,调用DLL中类对象.成员函数.(通过对象即可实现类成员函数 ...

  8. 阶段5 3.微服务项目【学成在线】_day02 CMS前端开发_08-vuejs研究-vuejs基础-v-if和v-for指令

    1.2.4 v-if和v-for <!DOCTYPE html> <html lang="en"> <head> <meta charse ...

  9. 图像分割-Mask Scoring R-CNN

    转载:https://zhuanlan.zhihu.com/p/58291808 论文链接:https://arxiv.org/abs/1903.00241 代码链接:https://github.c ...

  10. 推荐一个加载动图的网站loading.io

    推荐一个非常好玩的loading gif的资源网站:https://loading.io/ 里面有各种loading的动图.