oracle sql 经典例子 班级科目排名 去重

来源:互联网 发布:免费域名注册流程 编辑:程序博客网 时间:2024/06/11 10:27
Sql代码 复制代码 收藏代码
  1. ---------------------------------------------------------------------------------------  
  2. -------------------------------   经典成绩排名  ----------------------------------------  
  3. ---------------------------------------------------------------------------------------  
  4.   
  5. -- drop table L_Class   
  6. create table L_Class   
  7. (   
  8. c_id varchar2(20) not null primary key,   
  9. c_name varchar2(100),   
  10. c_no varchar2(50) not null unique -- 班级编号  
  11. );   
  12.   
  13. -- drop table L_Student   
  14. create table L_Student    
  15. (   
  16. s_id varchar2(20) not null primary key,   
  17. s_no varchar2(50) not null unique-- 学号  
  18. s_name varchar2(100),   
  19. c_no varchar2(50) not null -- 班级编号  
  20. );   
  21.   
  22. -- drop table L_Score;   
  23. create table L_Score   
  24. (   
  25. sc_id varchar2(20) not null primary key,   
  26. sc_score number,   
  27. sc_subject varchar2(50),   
  28. s_no varchar2(50) not null -- 学号  
  29. );   
  30.   
  31.   
  32. --  外键约束   
  33. alter table L_Student   
  34.       add constraint Class_Student_FK foreign key (c_no)   
  35.       references L_Class (c_no);   
  36.          
  37. alter table L_Score   
  38.       add constraint Score_Student_FK foreign key (s_no)   
  39.       references L_Student (s_no);   
  40.   
  41.   
  42.          
  43. -----------------------   
  44.   
  45. insert into L_Class values('1','1班','c1');   
  46. insert into L_Class values('2','2班','c2');   
  47. insert into L_Class values('3','3班','c3');   
  48.   
  49.   
  50. insert into L_Student values('1','s1','david1','c1');   
  51. insert into L_Student values('2','s2','david2','c1');   
  52. insert into L_Student values('3','s3','david3','c1');   
  53. insert into L_Student values('4','s4','tom1','c2');   
  54. insert into L_Student values('5','s5','tom2','c2');   
  55. insert into L_Student values('6','s6','tom3','c2');   
  56. insert into L_Student values('7','s7','kevin','c3');   
  57. insert into L_Student values('8','s8','jerry','c3');   
  58. insert into L_Student values('9','s9','fish','c3');   
  59. insert into L_Student values('10','s10','peter','c3');   
  60.   
  61.   
  62.   
  63. insert into L_Score values('1','61','语文','s1');   
  64. insert into L_Score values('2','62','语文','s2');   
  65. insert into L_Score values('3','63','语文','s3');   
  66. insert into L_Score values('4','64','语文','s4');   
  67. insert into L_Score values('5','65','语文','s5');   
  68. insert into L_Score values('6','66','语文','s6');   
  69. insert into L_Score values('7','67','语文','s7');   
  70. insert into L_Score values('8','68','语文','s8');   
  71. insert into L_Score values('9','69','语文','s9');   
  72. insert into L_Score values('10','70','语文','s10');   
  73. insert into L_Score values('11','71','数学','s1');   
  74. insert into L_Score values('12','72','数学','s2');   
  75. insert into L_Score values('13','73','数学','s3');   
  76. insert into L_Score values('14','74','数学','s4');   
  77. insert into L_Score values('15','75','数学','s5');   
  78. insert into L_Score values('16','76','数学','s6');   
  79. insert into L_Score values('17','77','数学','s7');   
  80. insert into L_Score values('18','78','数学','s8');   
  81. insert into L_Score values('19','79','数学','s9');   
  82. insert into L_Score values('20','80','数学','s10');   
  83.   
  84.   
  85.   
  86. select * from L_Student t;   
  87. select t.* from L_Class t;   
  88. select * from L_Score t;   
  89.   
  90.   
  91.   
  92. ---- (全年级) 各科 成绩排名   
  93. select *   
  94.   from (   
  95.         select s.s_name       姓名,   
  96.                 sc.sc_score   成绩,   
  97.                 --row_number() 行号,没有并列第一   
  98.                 --dense_rank() 有并列第一,接下来就是第二,   
  99.                 --rank() 有并列第一,接下来就是第三   
  100.                 dense_rank() over(partition by c.c_no, sc.sc_subject order by sc.sc_score desc) 排名,   
  101.                 sc.sc_subject 科目,   
  102.                 c.c_name      班级   
  103.           from L_Class c, L_Student s, L_Score sc   
  104.          where c.c_no = s.c_no   
  105.            and s.s_no = sc.s_no   
  106.         --and c.c_name = '3班'   --查询3班各科成绩排名   
  107.         ) tmp   
  108.  where tmp.排名 <= 3  -- 前 3 名  
  109.  --对结果集按照 班级、科目 排序   
  110.  order by tmp.班级 desc, tmp.科目 desc, tmp.成绩 desc;   
  111.   
  112.   
  113. -------------------------------------------------------------------------------------  
  114. ----------------------------    group by ... having ...    ---------------------------  
  115. -------------------------------------------------------------------------------------  
  116.   
  117. ---  查询成绩表中成绩 >64 的记录,成绩分组   
  118. select sc.sc_score from L_Score sc group by sc.sc_score having sc.sc_score > 75 order by sc.sc_score desc;   
  119.   
  120.   
  121.   
  122. -------------------------------------------------------------------------------------  
  123. ----------------------------    去除重复数据    -------------------------------------  
  124. -------------------------------------------------------------------------------------  
  125.   
  126. drop table L_User;   
  127.   
  128. create table L_User   
  129. (   
  130. u_id varchar2(20) not null primary key,   
  131. u_name varchar2(100),   
  132. u_age number   
  133. );   
  134.   
  135. insert into L_User values('1','david',20);   
  136. insert into L_User values('2','david',20);   
  137. insert into L_User values('3','kevin',23);   
  138. insert into L_User values('4','tom',25);   
  139. insert into L_User values('5','kevin',30);   
  140. insert into L_User values('6','kevin',20);   
  141.   
  142.   
  143. select t.*,rowid,rownum from L_User t where rownum < 5;   
  144.   
  145. --- 去重( 去除名字相同的记录,保留一条 )   
  146. delete from L_User u1 where u1.rowid < (select max(u2.rowid) from L_User u2 where u1.u_name=u2.u_name);   
  147. --- 去重( 去除年龄相同的记录,保留一条 )   
  148. delete from L_User u1 where u1.rowid < (select max(u2.rowid) from L_User u2 where u1.u_age=u2.u_age);   
  149. --- 去重( 去除名字、年龄都相同的记录,保留一条 )   
  150. delete from L_User u1 where u1.rowid < (select max(u2.rowid) from L_User u2 where u1.u_name=u2.u_name and u1.u_age=u2.u_age);  
0 0