zoukankan      html  css  js  c++  java
  • 高级查询之 over(partition by ... order by ... )

    create table test (id number(2), name varchar2(10), salary number(6,2));
    insert into test values (1,'Tom',120);
    insert into test values (2,'Ellen',240);
    insert into test values (2,'Joe',80);
    insert into test values (3,'Andy',200);
    insert into test values (3,'Kary',500);
    insert into test values (3,'Erick',1300);
    insert into test values (3,'Hou',40);
    insert into test values (3,'Mary',200);
    insert into test values (3,'Secooler',800);
    commit;

    select ID,
           name,
           salary,
           --LAG()和LEAD()统计函数可以在一次查询中取出同一字段的前N行的数据和后N行的值
           lag(salary,1) over(order by id) lag1_salary,
           lead(salary,2) over(order by id) lead2_salary,
           --last_value()和first_value()统计在一次查询中取出同一字段值最大或最小的数据
           last_value(name) over(partition by id order by salary rows between unbounded preceding and unbounded following) as highest_sal_name,
           last_value(salary) over(partition by id order by salary rows between unbounded preceding and unbounded following) as highest_sal,
           first_value(name) over(partition by id order by salary rows between unbounded preceding and unbounded following) as low_sal_name,
           first_value(salary) over(partition by id order by salary rows between unbounded preceding and unbounded following) as low_sal,
           row_number() over(partition by id order by salary desc )as rn,
           --rank()不连续排名 dense_rank()连续排名
           rank() over(partition by id order by salary desc ) as rank,
           dense_rank() over(partition by id order by salary desc ) as dense_rank
      from test
     order by ID, name;

    统计函数sum(),max(),min(),avg(),count()也可与之搭配使用

  • 相关阅读:
    分享一款日历的控件,适合新手
    解决ASP数据库连接出错,请检查连接字符串问题
    解决word中的Word experienced an error trying to open the file
    asp.net 利用jmail完成邮件的发送功能
    文章本天成 妙手偶得之 两句三年得 一吟双泪流
    有你有我有世界
    IT高薪者所具备的人格魅力
    谷歌史上10大发明精英
    坚持 乐观 方向 领悟 反思 跨越 距离
    Android之父Andy Rubin
  • 原文地址:https://www.cnblogs.com/willsun8023/p/4647669.html
Copyright © 2011-2022 走看看