CREATE TABLE T_STU( ID NUMBER(38) PRIMARY KEY, NAME VARCHAR2(20), GENDER NUMBER(1), GRADE NUMBER(4) );
插入数据
insert into t_stu values(1,'朱',1,100); insert into t_stu values(2,'张',0,98); insert into t_stu values(3,'高',1,95); insert into t_stu values(4,'林',1,95); insert into t_stu values(5,'刘',1,94); insert into t_stu values(6,'胡',0,97); insert into t_stu values(7,'陈',0,97); insert into t_stu values(8,'易',0,92);
求和的时候使用over
select t.* , sum(t.grade) over(partition by gender ) d from t_stu t
sum(GRADE ) over (partition by id order by gender) 性别连续求和,--各性别的成绩"连续"求和
sum(GRADE ) over (partition by GENDER ) 性别总和, -- 别统计的总和,同一性别总和不变
sum(GRADE ) over (order by id) 连续求和, --所有别的乘积"连续"求和
sum(GRADE ) over () 所有员工的薪水总和
求不同性别的排名:
SELECT s.*, DENSE_RANK() OVER(PARTITION BY s.gender ORDER BY s.grade DESC) dro FROM t_stu s
其中 DENSE_RANK()与rank()相反,只做名次的累加,不做跳跃。
rank()则是跳跃求和,比如有并列,则下一名次+2