zoukankan      html  css  js  c++  java
  • Oracle Partition By 的使用

    1.概述

    Parttion by 关键字是Oracle中分析性函数的一部分,它和聚合函数不同的地方在于它能够返回一个分组中的多条记录,儿聚合函数一般只有一条反映统计值的结果。

     

    2.使用方式

     

       场景:查询出每个部门工资最低的员工编号【每个部门可能有两个最低的工资员工】  

    create table TSALER
    (
      userid NUMBER(10),
      salary NUMBER(10),
      deptid NUMBER(10)
    )
    
    -- Add comments to the columns 
    comment on column TSALER.userid
      is '员工ID';
    comment on column TSALER.salary
      is '工资';
    comment on column TSALER.deptid
      is '部门ID';
    
    insert into TSALER (工号, 工资, 部门编号)
    values (1, 200, 1);
    
    insert into TSALER (工号, 工资, 部门编号)
    values (2, 2000, 1);
    
    insert into TSALER (工号, 工资, 部门编号)
    values (3, 200, 1);
    
    insert into TSALER (工号, 工资, 部门编号)
    values (4, 1000, 2);
    
    insert into TSALER (工号, 工资, 部门编号)
    values (5, 1000, 2);
    
    insert into TSALER (工号, 工资, 部门编号)
    values (6, 3000, 2);
    View Code

      

    查询结果:

    2.1方法一

    select tsaler.* from tsaler 
    inner join(select min(salary) as salary,deptid from tsaler group by deptid) c
    on tsaler.salary=c.salary and tsaler.deptid=c.deptid 

    2.2方法二

    select * from tsaler 
    inner join(select min(salary) as salary,deptid from tsaler group by deptid) c
    using(salary,deptid)

    2.3方法三

    --row_number() 顺序排序
    select row_number() over(partition by deptid order by salary) my_rank ,deptid,USERID,salary from tsaler;
    --rank() (跳跃排序,如果有两个第一级别时,接下来是第三级别)
    select rank() over(partition by deptid order by salary) my_rank,deptid,USERID,salary from tsaler;
    --dense_rank()(连续排序,如果有两个第一级别时,接下来是第二级)
    select dense_rank() over(partition by deptid order by salary) my_rank,deptid,USERID,salary from tsaler;
    -------方案3解决方案
    select * from (select rank() over(partition by deptid order by salary) my_rank,deptid,USERID,salary from tsaler) where my_rank=1;
    select * from (select dense_rank() over(partition by deptid order by salary) my_rank,deptid,USERID,salary from tsaler) where my_rank=1;

     

  • 相关阅读:
    找数字(递归,二分查找)
    P1759 通天之潜水(不详细,勿看)(动态规划递推,组合背包,洛谷)
    第五讲 二维费用的背包问题(粗糙,勿点)
    VIM基础操作方法汇总
    P2347 砝码称重(动态规划递推,背包,洛谷)
    第三讲 多重背包问题(对背包九讲的学习)
    第二讲 完全背包问题(对背包九讲的学习)
    python 日期、时间、字符串相互转换
    Resource注解无法导入依赖使用javax.annotation的注解类
    Spring的配置文件找不到元素 'beans' 的声明
  • 原文地址:https://www.cnblogs.com/jak-black/p/4210653.html
Copyright © 2011-2022 走看看