over(Partition by...) 详细用法

常用的分析函数如下所列:
Pw"XG*J rj.W0row_number() over(partition by ... order by ...)
H*v}I*Z/_:SkP0rank() over(partition by ... order by ...)CNOUG博客首页.Z&]q /7yzC'K
dense_rank() over(partition by ... order by ...)CNOUG博客首页 Lz] w9c0G,y U*A]w
count() over(partition by ... order by ...)
"z+a"c*n-]uH0max() over(partition by ... order by ...)CNOUG博客首页*`z#G/R?/Q[}J
min() over(partition by ... order by ...)CNOUG博客首页u/H]O}v]
sum() over(partition by ... order by ...)
[}!_ [M%Y Yp0avg() over(partition by ... order by ...)
+lynTx5i5rBL l0first_value() over(partition by ... order by ...)
J*Ou#f?V4L8w0last_value() over(partition by ... order by ...)CNOUG博客首页/ib,M6AE
lag() over(partition by ... order by ...)
4f v2y1FBH3F0lead() over(partition by ... order by ...)

示例:CNOUG博客首页i A7XFi?o
14:33:29 SQL> select type,qty from test;
u6PD7r$O u {a0TYPE QTY
)u7h,]V6U0---------- ----------
qD7lEuB0UG!DHq01 3
c$v8O4p.z9LL8b5X$O01 6CNOUG博客首页5o1`c J |(?
2 5CNOUG博客首页E7Zw|(]'t
2 9CNOUG博客首页2q&m L2e?5a
2 7

14:33:36 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;CNOUG博客首页ra/~&P3iN
TYPE QTY CNT2CNOUG博客首页E1T?.sW]@7z
---------- ---------- ------------
Z.J"]q{(L01 3 1/2CNOUG博客首页Z P5j-Q4C!?e
1 6 2/2CNOUG博客首页5?;X/c3bi aP*O
2 5 1/3CNOUG博客首页D j5S`N4{0l?P
2 7 2/3
S{ecDK#BJO02 9 3/3

 

SQL> select * from test;

ID MC
A$Fq#` `?r0---------- --------------------------------------------------
qf5s)cA'P0g0gp01 11111
)M"i IrBs9L02 22222
${%IPE/M03 33333
9J-^4p-i&a&mPI.`eU04 44444

SQL>CNOUG博客首页&X%B?j+f"dm|n
SQL> select t.id,mc,to_char(b.rn)||'/'||t.idCNOUG博客首页 U6W:s)WeF+Q.G:Mb!SU
2 from test t,CNOUG博客首页XH^'o*M5`1Co
3 (select rownum rn from (select max(to_number(id)) mid from test) connect by rownum <=mid ) b
%Cz Ao o-f04 where b.rn<=to_number(t.id)CNOUG博客首页dGP,W4x/l9W
5 order by id,3CNOUG博客首页 Z%M P.T,|
6 /

ID MC TO_CHAR(B.RN)||'/'||T.IDCNOUG博客首页9z~^@WW3c*L.c
---------- -------------------------------------------------- ---------------------------------------------------
?t(rL5m3R I9fO"iw01 11111 1/1CNOUG博客首页 zZ/3q:h
2 22222 1/2CNOUG博客首页'?/r{#Q(xUo
2 22222 2/2CNOUG博客首页 tn6S;m?mQ.c
3 33333 1/3CNOUG博客首页k `o-M/!?;aG7a9N+Q
3 33333 2/3
!LY:J/A+N03 33333 3/3
J3A o-`dT04 44444 1/4
o+f!@ s7[Aw D8C#D0

你可能感兴趣的:(PL/SQL)