zoukankan      html  css  js  c++  java
  • Sql中的分页查询

    --分页查询(三种情况)
    --1.取前X条数据
    --rownum的关键字,代表行号,是系统自动生成的,是从1开始分配的
    select e.* from emp e where rownum <= 5

    --2.取中间X条数据
    select t01.*
    from (select e.*, rownum r from emp e) t01
    where t01.r > 5
    and t01.r <= 10

    --3.对数据要进行排序,然后再分页
    --搜索出来薪资最高的前五名员工
    select t01.*
    from (select e.* from emp e order by e.sal desc) t01
    where rownum < 6

    --取出薪资最高的6~10名
    select tt01.* from (select t01.*, rownum r
    from (select e.* from emp e order by e.sal desc) t01) tt01
    where tt01.r > 5
    and tt01.r < 11

    --1.求平均薪水最高的部门的部门编号
    select e.deptno, avg(e.sal)
    from emp e
    group by e.deptno
    having avg(e.sal) >= all (select avg(e.sal) from emp group by e.deptno)

    --用行号来解决
    select *
    from (select e.deptno, avg(e.sal) vsal
    from emp e
    group by e.deptno
    order by vsal desc) t01
    where rownum = 1


    --2.求部门平均薪水的等级
    select t01.deptno, t01.vsal, sg.grade
    from (select e.deptno, avg(e.sal) vsal from emp e group by e.deptno) t01
    join salgrade sg
    on t01.vsal between sg.losal and sg.hisal

    --3.求部门平均的薪水等级
    select e.deptno,avg( sg.grade) vgrade
    from emp e
    join salgrade sg
    on e.sal between sg.losal and sg.hisal
    group by e.deptno

  • 相关阅读:
    NOI2017 退役记
    bzoj2590[Usaco2012 Feb]Cow Coupons
    bzoj2215[POI2011]Conspiracy
    bzoj2115[WC2011]Xor
    bzoj4754[JSOI2016]独特的树叶
    SQL外连接与条件 left outer join + WHERE/AND 区别
    IBM Datastage
    无法在MS SQL SERVER MANAGEMENT中打开SSIS实例-解决办法
    SSIS 教学教程
    SQL SERVER 2019 电子书下载
  • 原文地址:https://www.cnblogs.com/su-chu-zhi-151/p/11196922.html
Copyright © 2011-2022 走看看