oracle--高级使用(merge)(递归START WITH)分析函数over
1、俩种表复制语句
SELECT INTO和INSERT INTO SELECT两种表复制语句
CT: create table <new table> as select * from <exists table>
要求目标表不存在,因为在插入时会自动创建表,并将查询表中指定字段数据复制到新建的表中
IS: insert into table2 (f1,f2,..) select v1,v2.... from table1
要求目标表table2必须存在,由于目标表table2已经存在,所以我们除了插入源表 table1的字段外,还可以插入常量
2、merge into 用法
MERGE INTO用法:
merge into表A
using与表A产生关联字段值
on进行和表A关联
when matched then
update set...
when not matched then
insert ...) values
create table PRODUCTS(PRODUCT_ID INTEGER, REQ_NO VARCHAR(32), PRODUCT_NAME VARCHAR2(60), CATEGORY VARCHAR2(60));
insert into PRODUCTS values (1501, '001', 'vIVITAR 35Mм', 'ELECTRNCS');
insert into PRODUCTS values (1502, '002', 'oLYMPUS I85o', 'ELECTRNCS');
insert into PRODUCTS values (1600, '003', 'PIAY GYм', 'тoYS');
insert into PRODUCTS values (1601, '003', 'LAMAZE', 'moYs');
insert into PRODUCTS values (1717, '001', 'HARRY POTTER', 'DVD');
insert into PRODUCTS values (1666, '002', 'HARRY POTTER', 'DVD');
commit; --drop table PRODUCTS;
select * from PRODUCTS; merge into PRODUCTS a
using (select 1717 product_id, '001' req_no from dual) b
on (a.product_id = b.product_id and a.req_no = b.req_no) when matched then
update set product_name = '进行更新啦' , category = '新的category'
when not matched then
insert (product_id ,req_no , product_name , category)
values(1717, '002' , '新产品' , 'CCA');
commit;
3、递归函数
select * from emp
--where empno = 79391 or
start with empno = 7369 or empno = 7934
connect by prior mgr = empno
order by sal desc;
--PID在前面ID在后 (向上查询) --ID在前PI在后(向下查询)
4、分析函数
over函数
over partition by组合
over partition by order by组合
row_number函数
rollup函数
cube函数
grouping函数
over是分组函数
order by 是按什么连续求和
partition by 按什么分区
select deptno,ename, sal,
sum(sal) over (order by deptno) 连续求和,
sum(sal) over() 总和,
100*round(sal/sum(sal) over (),5) 份额
from emp;
select deptno,ename,sal,
sum(sal) over (partition by deptno order by ename) 部门连续求和,
sum(sal) over (partition by deptno) 部门总和,
100*round(sal/sum(sal) over (partition by deptno),4) "部门份额(%)",
sum(sal) over (order by deptno,ename) 连续求和,
sum(sal) over() 总和,
100*round(sal/sum(sal) over (),4) "份额(%)"
from emp;
求和规则有按部门分区的,有不分区的例子
5、row_number()分组排名
create table employee (empid int ,deptid int ,salary decimal (10,2));
insert into employee values (1,10,5500.00);
insert into employee values (2,10,4500.00);
insert into employee values (3,20,1900.00);
insert into employee values (4,20, 4800.00);
insert into employee values (5,40, 6500.00);
insert into employee values (6,40,14500.00);
insert into employee values (7,40,44500.00);
insert into employee values (8,50,6500.00);
insert into employee values (9, 50,7500.00);
commit; select * from employee; select e.*, row_number() over(partition by deptid order by empid) rank from employee e;
6、rollup()
在group by 分组上在加上对earnmonth的汇总统计
create table earnings--打工赚线表
(earnmonth varchar2 (6), --打工月份
area varchar2 (20),--打工地区
sno varchar2 (10),--打工者續号
sname varchar2 (20), --打工者姓名
times int,--本月打工次数
singieincome number (10,2), --、每次漠多少钱
personincome number (10,2) --当月总收入
);
-- truncate table earnings;
insert into earnings values ('200912','北平','511601','大',11,30,11*30);
insert into earnings values ('200912','北平','511602','大',8,25,8*25);
insert into earnings values ('200912','北平','511603','小素',30,6.25,30*6.25);
insert into earnings values ('200912','北平','511604','大亮',16,8.25,16*8.25);
insert into earnings values ('200912','北平','511605','搜藏',30,11,30*11);
insert into earnings values ('200912','金展','511301','小玉',15,12.25,15*12.25);
insert into earnings values ('200912','金展','511302','小凡',27,16.67,27*16.67);
insert into earnings values ('200912','金展','511303','小妮',7,39.33,7*99.33);
insert into earnings values ('200912','金展','511304','小角',0,18,0);
insert into earnings values ('200912','金展','511305','儿',11,9.88,11*9.88); insert into earnings values ('201001','北平','511601','大鬼',0,30,0);
insert into earnings values ('201001','北平','511602','大肌',14,25,14*25);
insert into earnings values ('201001','北平','511603','小',19,6.25,19*6.25);
insert into earnings values ('201001','北平','511604','大亮',7,8.25,7*8.25);
insert into earnings values ('201001','北平','511605','硬載',21,11,21*11);
insert into earnings values ('201001','金展','511301','小玉',6,12,25.6*12.25) ;
insert into earnings values ('201001','金展','511302','小凡',17,16.67,17*16.67);
insert into earnings values ('201001','金展','511303','小班',27,33.33,27*39.3);
insert into earnings values ('201001','金展','511304','小角',16,18,16*18);
insert into earnings values ('201001','金展','511305','儿',11,9.8,11*9.88);
commit; select earnmonth, area, sum(personincome)
from earnings
group by earnmonth, area ; select earnmonth, area, sum(personincome)
from earnings
group by rollup(earnmonth, area);
7、cube()分组
select earnmonth, area, sum(personincome)
from earnings
group by cube(earnmonth, area)
order by earnmonth, area;
8、grouping()
别名
select earnmonth,
(case
when ((grouping(area) = 1) and (grouping(earnmonth) = 0)) then
'月份小计'
when ((grouping(area) = 1) and (grouping(earnmonth) = 1)) then
'总计'
else
area
end) as area, sum(personincome)
from earnings
group by rollup(earnmonth, area);
9、排名 rank() dense_rank() row_number()
rank() 相同的值排名相同,但是下一位排名需要算上前面的,跳跃式排名
elect earnmonth 月份, area 地区, sname 打工者, personincome 收入,
rank() over(partition by earnmonth,area order by personincome desc) 排名
from earnings;
select earnmonth 月份, area 地区, sname 打工者, personincome 收入,
dense_rank() over(partition by earnmonth,area order by personincome desc) 排名
from earnings;
select earnmonth 月份, area 地区, sname 打工者, personincome 收入,
row_number() over(partition by earnmonth,area order by personincome desc) 排名
from earnings;
10、sum()
select earnmonth 月份, area 地区, sname 打工者, personincome 收入,
sum(personincome) over(partition by sname order by personincome desc) 总收入
from earnings;
11、多行函数参与分组
select distinct earnmonth 月份, area 地区,
max(personincome) over(partition by earnmonth, area) 最高值,
min(personincome) over(partition by earnmonth, area) 最低值,
avg(personincome) over(partition by earnmonth, area) 平均值,
sum(personincome) over(partition by earnmonth, area) 总收入
from earnings;
oracle--高级使用(merge)(递归START WITH)分析函数over的更多相关文章
- oracle 高级函数2
原 oracle 高级函数 2017年08月17日 16:44:19 阅读数:1731 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/u013278 ...
- oracle 高级函数
原 oracle 高级函数 2017年08月17日 16:44:19 阅读数:1731 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/u013278 ...
- oracle 高级分组
oracle 高级分组 博客分类: 数据库基础 oraclesql 10.高级分组 本章目标: 对于增强的group by需要掌握: 1.使用rollup(也就是roll up累计的意思)操作产生s ...
- oracle高级查询(实例基于scott用户四张表)
oracle高级查询(实例基于scott用户四张表) 分组查询 多表查询 子查询 综合实例 ====================================================== ...
- Oracle基础之Merge into
Merge into语句是Oracle9i新增的语法,用来合并UPDATE和INSERT语句. 通过MERGE语句,根据一张表或多表联合查询的连接条件对另外一张表进行查询,连接条件匹配上的进行UPDA ...
- 在oracle中使用merge into实现更新和插入数据
目录 oracle中使用merge into DUAL表解释 使用场景 用法 单表 多表 oracle中使用merge into DUAL表解释 在Oracle数据库中,dual是Oracle中的一个 ...
- 39.oracle高级篇
标题说是高级篇,其实也就是相对于基础篇来说的,也不是很深奥,自己平时工作中也都会用到,这里回忆的并不是特别冷门的知识,不要掉以轻心,以为“高级”就觉得工作中不会用到了. 一.select into 和 ...
- oracle数据匹配merge into
来源于:http://blog.csdn.net/vtopqx/article/details/50633865 前言: 很久之前,估计在2010年左右在使用Oralce,当时有个需求就是需要对两个表 ...
- Oracle高级查询之OVER
注释:为了方便大家学习和测试,所有的例子都是在Oracle自带用户Scott下建立的 oracel的高级用法:rank()/dense_rank() over(partition by ...orde ...
- [转]详解Oracle高级分组函数(ROLLUP, CUBE, GROUPING SETS)
原文地址:http://blog.csdn.net/u014558001/article/details/42387929 本文主要讲解 ROLLUP, CUBE, GROUPING SETS的主要用 ...
随机推荐
- 磁盘IO性能优化-实践
RAID卡缓存策略调整 原因详解 操作实例 I/O 调度算法 文件系统journal 磁盘挂载参数 操作实例 性能数据对比 RAID卡缓存策略调整 可以将RAID卡缓存策略由No Write Cach ...
- The Preliminary Contest for ICPC Asia Xuzhou 2019 E. XKC's basketball team (线段树)
题目链接:https://nanti.jisuanke.com/t/41387 题目大意:对于给定序列,求出对于每个位置求出比该数大于m的最靠右的位置. 思路:首先对序列进行离散化,然后对于每个数的下 ...
- 关于数据库抛出异常:Incorrect string value: '\xE1\x...' for column '字段名' at row 1 问题的解决方法
打开sql,进行语句编辑 ENGINE=InnoDB DEFAULT CHARSET=utf8;字符集设置utf-8编码
- <转> thinkPHP的常用配置项2
'URL_PATHINFO_DEPR'=>'-',//修改URL的分隔符'TMPL_L_DELIM'=>'<{', //修改左定界符'TMPL_R_DELIM'=>'}> ...
- DevExpress ASP.NET Core Controls 2019发展蓝图(No.6)
本文主要为大家介绍DevExpress ASP.NET Core Controls 2019年的官方发展蓝图,更多精彩内容欢迎持续收藏关注哦~ [DevExpress ASP.NET Controls ...
- Centos 6.4 x86_64 最小化安装后的优化——还需要整理
Centos 6.4 x86_64 最小化安装后的优化 购买了服务器以后要做的第一件事就是安装操作系统了,这里推荐安装 Centos 6.4 x86_64,安装系统时要选择最小化安装(不需要图 ...
- npm cache clean --force
当出现这个问题时npm ERR! Unexpected end of JSON input while parsing near '...,"dist":{"shasum ...
- POJ 2104 K-th Number ( 求取区间 K 大值 || 主席树 || 离线线段树)
题意 : 给出一个含有 N 个数的序列,然后有 M 次问询,每次问询包含 ( L, R, K ) 要求你给出 L 到 R 这个区间的第 K 大是几 分析 : 求取区间 K 大值是个经典的问题,可以使用 ...
- springboot集成mongoDB需要认证
报错: Mon Nov 25 01:09:48 CST 2019 There was an unexpected error (type=Internal Server Error, status=5 ...
- 【CF1256F】Equalizing Two Strings(逆序对)
题意:给定两个长度均为n且由小写字母组成的字符串,可以进行若干次操作,每次从两个串中分别选一个长度相等的子串进行翻转,问是否存在能使两串相等的一系列操作方案 n<=2e5 思路:首先如果每种字母 ...