procedure的over(partition by ) function
转载自:http://www.2cto.com/database/201310/249722.html
1:统计某商店的营业额。
date
sale
1 20
2 15
3 14
4 18
5 30
规则:按天统计:每天都统计前面几天的总额
得到的结果:
DATE
SALE
SUM
----- -------- ------
1 20 20
--1天
2 15 35
--1天+2天
3 14 49
--1天+2天+3天
4 18 67 .
5 30 97 .
2:统计各班成绩第一名的同学信息
NAME
CLASS S
----- ----- ----------------------
fda 1 80
ffd 1 78
dss 1 95
cfe 2 74
gds 2 92
gf 3 99
ddd 3 99
adf 3 45
asdf 3 55
3dd 3 78
通过:
--
select
*
from
(
select
name
,class,s,rank()over(partition
by
class
order
by
s
desc
) mm
from
t2
)
where
mm=1
--
得到结果:
NAME
CLASS S MM
----- ----- ---------------------- ----------------------
dss 1 95 1
gds 2 92 1
gf 3 99 1
ddd 3 99 1
注意:
1.在求第一名成绩的时候,不能用row_number(),因为如果同班有两个并列第一,row_number()只返回一个结果
2.rank()和dense_rank()的区别是:
--rank()是跳跃排序,有两个第二名时接下来就是第四名
--dense_rank()l是连续排序,有两个第二名时仍然跟着第三名
3.分类统计 (并显示信息)
A B C
-- -- ----------------------
m a 2
n a 3
m a 2
n b 2
n b 1
x b 3
x b 2
x b 4
h b 3
select
a,c,
sum
(c)over(partition
by
a)
from
t2
得到结果:
A B C
SUM
(C)OVER(PARTITIONBYA)
-- -- ------- ------------------------
h b 3 3
m a 2 4
m a 2 4
n a 3 6
n b 2 6
n b 1 6
x b 3 9
x b 2 9
x b 4 9
如果用
sum
,
group
by
则只能得到
A
SUM
(C)
-- ----------------------
h 3
m 4
n 6
x 9
无法得到B列值
=====
select
*
from
test
数据:
A B C
1 1 1
1 2 2
1 3 3
2 2 5
3 4 6
---将B栏位值相同的对应的C 栏位值加总
select
a,b,c,
SUM
(C) OVER (PARTITION
BY
B) C_Sum
from
test
A B C C_SUM
1 1 1 1
1 2 2 7
2 2 5 7
1 3 3 3
3 4 6 6
---如果不需要已某个栏位的值分割,那就要用 null
eg: 就是将C的栏位值summary 放在每行后面
select
a,b,c,
SUM
(C) OVER (PARTITION
BY
null
) C_Sum
from
test
A B C C_SUM
1 1 1 17
1 2 2 17
1 3 3 17
2 2 5 17
3 4 6 17
求个人工资占部门工资的百分比
SQL>
select
*
from
salary;
NAME
DEPT SAL
---------- ---- -----
a 10 2000
b 10 3000
c 10 5000
d 20 4000
SQL>
select
name
,dept,sal,sal*100/
sum
(sal) over(partition
by
dept) percent
from
salary;
NAME
DEPT SAL PERCENT
---------- ---- ----- ----------
a 10 2000 20
b 10 3000 30
c 10 5000 50
d 20 4000 100
二:开窗函数
开窗函数指定了分析函数工作的数据窗口大小,这个数据窗口大小可能会随着行的变化而变化,举例如下:
1:
over(
order
by
salary) 按照salary排序进行累计,
order
by
是个默认的开窗函数
over(partition
by
deptno)按照部门分区
2:
over(
order
by
salary range
between
5 preceding
and
5 following)
每行对应的数据窗口是之前行幅度值不超过5,之后行幅度值不超过5
例如:对于以下列
aa
1
2
2
2
3
4
5
6
7
9
sum
(aa)over(
order
by
aa range
between
2 preceding
and
2 following)
得出的结果是
AA
SUM
---------------------- -------------------------------------------------------
1 10
2 14
2 14
2 14
3 18
4 18
5 22
6 18
7 22
9 9
就是说,对于aa=5的一行 ,
sum
为 5-1<=aa<=5+2 的和
对于aa=2来说 ,
sum
=1+2+2+2+3+4=14 ;
又如 对于aa=9 ,9-1<=aa<=9+2 只有9一个数,所以
sum
=9 ;
3:其它:
over(
order
by
salary
rows
between
2 preceding
and
4 following)
每行对应的数据窗口是之前2行,之后4行
4:下面三条语句等效:
over(
order
by
salary
rows
between
unbounded preceding
and
unbounded following)
每行对应的数据窗口是从第一行到最后一行,等效:
over(
order
by
salary range
between
unbounded preceding
and
unbounded following)
等效
over(partition
by
null
)
常用的分析函数如下所列:
row_number() over(partition
by
...
order
by
...)
rank() over(partition
by
...
order
by
...)
dense_rank() over(partition
by
...
order
by
...)
count
() over(partition
by
...
order
by
...)
max
() over(partition
by
...
order
by
...)
min
() over(partition
by
...
order
by
...)
sum
() over(partition
by
...
order
by
...)
avg
() over(partition
by
...
order
by
...)
first_value() over(partition
by
...
order
by
...)
last_value() over(partition
by
...
order
by
...)
lag() over(partition
by
...
order
by
...)
lead() over(partition
by
...
order
by
...)
示例
SQL>
select
type,qty
from
test;
TYPE QTY
---------- ----------
1 6
2 9
SQL>
select
type,qty,to_char(row_number() over(partition
by
type
order
by
qty))||
'/'
||to_char(
count
(*) over(partition
by
type))
as
cnt2
from
test;
TYPE QTY CNT2
---------- ---------- ------------
3 1/2
1 6 2/2
2 5 1/3
7 2/3
2 9 3/3
SQL>
select
*
from
test;
---------- -------------------------------------------------
1 11111
2 22222
3 33333
4 44444
SQL>
select
t.id,mc,to_char(b.rn)||
'/'
||t.id)e
2
from
test t,
(
select
rownum rn
from
(
select
max
(to_number(id)) mid
from
test)
connect
by
rownum <=mid ))L
4
where
b.rn<=to_number(t.id)
order
by
id
ID MC TO_CHAR(B.RN)||
'/'
||T.ID
--------- -------------------------------------------------- ---------------------------------------------------
1 11111 1/1
2 22222 1/2
2 22222 2/2
3 33333 1/3
3 33333 2/3
3 33333 3/3
44444 1/4 44444 2/4
4 44444 3/4CNOUG4 44444 4/4
10
rows
selected
*******************************************************************
1.
select
deptno,row_number() over(partition
by
deptno
order
by
sal)
from
emp
order
by
deptno;
2.
select
deptno,rank() over (partition
by
deptno
order
by
sal)
from
emp
order
by
deptno;
3.
select
deptno,dense_rank() over(partition
by
deptno
order
by
sal)
from
emp
order
by
deptno;
4.
select
deptno,ename,sal,lag(ename,1,
null
) over(partition
by
deptno
order
by
ename)
from
emp ord er
by
deptno;
5.
select
deptno,ename,sal,lag(ename,2,
'example'
) over(partition
by
deptno
order
by
ename)
from
em p
order
by
deptno;
6.
select
deptno, sal,
sum
(sal) over(partition
by
deptno)
from
emp;
--每行记录后都有总计值 select deptno, sum(sal) from emp group by deptno;
7. 求每个部门的平均工资以及每个人与所在部门的工资差额
select
deptno,ename,sal ,
round(
avg
(sal) over(partition
by
deptno))
as
dept_avg_sal,
round(sal-
avg
(sal) over(partition
by
deptno))
as
dept_sal_diff
from
emp;
procedure的over(partition by ) function的更多相关文章
- [独孤九剑]Oracle知识点梳理(六)数据库常用对象之Procedure、function、Sequence
本系列链接导航: [独孤九剑]Oracle知识点梳理(一)表空间.用户 [独孤九剑]Oracle知识点梳理(二)数据库的连接 [独孤九剑]Oracle知识点梳理(三)导入.导出 [独孤九剑]Oracl ...
- Delphi7中的函数与过程(Function and Procedure)
1.锁住空间的位置,可以选择Edit--->Lock component ,也可以在窗体设计面板下面找到组件排版功能栏,第二排里面有个带锁的图标,表示组件可以被锁住.点击一下,组件的大小和位置就 ...
- [转]Dynamic SQL & Stored Procedure Usage in T-SQL
转自:http://www.sqlusa.com/bestpractices/training/scripts/dynamicsql/ Dynamic SQL & Stored Procedu ...
- How to throw an error in MySql procedure?
http://stackoverflow.com/questions/4862911/how-to-throw-an-error-in-mysql-procedure 9down votefavori ...
- Delphi 中的 procedure of object
转载:http://www.cnblogs.com/ywangzi/archive/2012/08/28/2659811.html 总结:TMyEvent = procedure of object; ...
- 一个不简单的Procedure body例子
create or replace package body CountBankData_20150617 is type cursorCommon is ref cursor; --游标类型 str ...
- MySQL与Oracle 差异比较之五存储过程&Function
存储过程&Function 编号 类别 ORACLE MYSQL 注释 1 创建存储过程语句不同 create or replace procedure P_ADD_FAC( id_fac ...
- Delphi 中的 procedure of object (类方法存在一个隐藏参数self),简单深刻 good
其实要了解这些东西,适当的学些反汇编,WINDOWS内存管理机制,PE结构,看下李维的VCL架构剖析可以很好理解type TMyEvent = procedure of object;这是一种数据类型 ...
- 存储过程&Function
存储过程&Function 编号 类别 ORACLE MYSQL 注释 1 创建存储过程语句不同 create or replace procedure P_ADD_FAC( id_fac ...
随机推荐
- Android开发错误信息收集
android sdk 无法更新或更新太慢 备注信息:sdk manager中加入mirrors.opencas.org,强迫http方式 ADT下载地址:http://dl.google.com/a ...
- A Tour of Go Interfaces
An interface type is defined by a set of methods. A value of interface type can hold any value that ...
- [置顶] HTML语义和前端架构
关于语义学 语义学是研究符号和意义之间的关系以及它们表示的内容.在语言学中,则主要是研究符号(例如单词,短语或者语音)在语言中所表达的意义.而在前端开发时,语义学则更多的关注HTML元素,属性以及它的 ...
- IOS开发--C语言入门--如何结合Terminal和Vim开发C语言程序
一直以来都想着挤出时间,记录开发之路的点点滴滴(现在记录已成回忆),和大家一起分享,开发人员总是在沟通和相互学习中提高自身的能力.路过的大神也好初学者也罢,若发现文章中又观点不对的,还望大家指出. 现 ...
- NGUI学习笔记(一):官方视频学习记录
学习NGUI一直断断续续的,目前打算做一个总结的笔记. 我使用的是比较老的3.6.0版本. 1.使用NGUI,需要开启“Edit”->“Project Settings”->“Physic ...
- CentOS安装卸载memcache及JAVA示例
原文地址:http://www.cnblogs.com/zhongshengzhen/ 先安装libevent,memcached依赖libevent的lib [root@VM_64_81_c ...
- 第一个struts案例及分析
软件中的框架,是一种半成品: 我们项目开发需要在框架的基础上进行!因为框架已经实现了一些功能,这样就可以提高开发效率! Struts2 = struts1 + xwork (struts是基于MV ...
- 教你50招提升ASP.NET性能(一):缓存是最后的手段
(1)Caching is a last resort 招数1: 缓存是最后的手段 Projects that use multiple levels of cache often demonstra ...
- Icon specified in the Info.plist not found under the top level app wrapper: Icon.png
For some reason the (possibly when adding multiple icons and changing the file?) the item gets moved ...
- Swipe2.1更新——移动Web内容滑块
Swipe JS 是一个轻量级(3.7 kb) mobile slider,支持 1:1 触摸移动(基于精确的触摸位置的内容滑动). 但是我使用一段时间后发现两个bug,所以在官方2.0(官网http ...