oracle 语法 over 的使用

SQL> select  deptno, dname,lag(dname,2) over (order   by   dname) lag from   dept;--按dname后退二行显示

DEPTNO DNAME LAG
------ -------------- --------------
10 ACCOUNTING
40 OPERbTIONS
20 RESEARCH ACCOUNTING
30 SALES OPERbTIONS

SQL> select deptno, dname,lead(dname,2) over (order by dname) lag from dept;--与上相反

DEPTNO DNAME LAG
------ -------------- --------------
10 ACCOUNTING RESEARCH
40 OPERbTIONS SALES
20 RESEARCH
30 SALES
对这个表进行操作
NAME CLASS SCORE
---------- ---------- ----------
ff 1 97
gg 1 89
ll 1 96
jj 2 89
oo 2 87
ii 1 98
kk 2 93
uu 3 97
rr 3 95
ee 3 92
yy 2 90
mm 4 100
nn 4 98
pp 1 98
SQL> select t.name,t.class,t.score from (select name,class,score ,rank() over ( partition by class order by score desc) m from c_score )t where m=1;--求各班成绩最高的人

NAME CLASS SCORE
---------- ---------- ----------
ii 1 98
pp 1 98
kk 2 93
uu 3 97
mm 4 100
--rank()是跳跃排序,有两个第二名时接下来就是第四名
--dense_rank()l是连续排序,有两个第二名时仍然跟着第三名
SQL> select name,class,score,sum(score) over (partition by class) total from c_score;显示各班总分并显示出SCORE列

NAME CLASS SCORE TOTAL
---------- ---------- ---------- ----------
pp 1 98 478
ii 1 98 478
ll 1 96 478
ff 1 97 478
gg 1 89 478
oo 2 87 359
jj 2 89 359
kk 2 93 359
yy 2 90 359
ee 3 92 284
rr 3 95 284
uu 3 97 284
nn 4 98 198
mm 4 100 198

SQL> select class,sum(score) from c_score group by class;这样不能显示出score这列

CLASS SUM(SCORE)
---------- ----------
1 478
3 284
2 359
4 198

SQL> select name,class,score,round(score*100/sum(score) over (partition by class),2)||'%' total from c_score;
个人分数占班级总分数的百分比
NAME CLASS SCORE TOTAL
---------- ---------- ---------- -----------------------------------------
pp 1 98 20.5%
ii 1 98 20.5%
ll 1 96 20.08%
ff 1 97 20.29%
gg 1 89 18.62%
oo 2 87 24.23%
jj 2 89 24.79%
kk 2 93 25.91%
yy 2 90 25.07%
ee 3 92 32.39%
rr 3 95 33.45%
uu 3 97 34.15%
nn 4 98 49.49%
mm 4 100 50.51%

select name,class,score,sum(score) over (order by score range between 2 preceding and 2 following) sum_range from c_score;对应的sum_range是score-1<=score<=score+2 这段间的和
over(order by salary rows between 2 preceding and 4 following)
每行对应的数据窗口是之前2行,之后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);
以上三条语句等效


曾经一道面试题当时没有做出来,意思同如下,在这个表中如果class与score相同,就考虑这行数据多余,删除多余行,就随便保留一行。
NAME CLASS SCORE
---------- ---------- ----------
ff 1 97
gg 1 89
ll 1 96
jj 2 89
oo 2 87
ii 1 98
kk 2 93
uu 3 97
rr 3 95
ee 3 92
yy 2 90
mm 4 100
nn 4 98
pp 1 98
fft 1 97
ggt 1 89
oot 2 87
kkt 2 93
ffff 1 97
SQL> delete from c_score t where rowid in(select rowid from (select rowid ,row_number() over (partition by class,score order by class)dup_num from c_score)t where t.dup_num>1);

6 rows deleted

SQL> select * from c_score;

NAME CLASS SCORE
---------- ---------- ----------
ff 1 97
gg 1 89
ll 1 96
jj 2 89
kk 2 93
uu 3 97
rr 3 95
ee 3 92
yy 2 90
mm 4 100
nn 4 98
pp 1 98
oot 2 87
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值