首页 > 数据库 >Oracle查前几条记录方法

Oracle查前几条记录方法

时间:2023-11-07 12:03:14浏览次数:34  
标签:查前 insert 几条 over order mynumber Oracle id select

Oralce的示例:

1. 最佳选择:利用分析函数

       row_number() over ( partition by col1 order by col2 )
                               比如想取出100-150条记录,按照tname排序

               select tname,tabtype from (
                               select tname,tabtype,row_number() over ( order by tname ) rn from tab
                ) where rn between 100 and 150;

2. 使用rownum 虚列

         select tname,tabtype from (
                    select tname,tabtype,rownum rn from tab where rownum <= 150                  ) where rn >= 100;

注释:使用虚列时不能基于整个记录集合来进行排序,假如指定了order by子句,排序的是选出来的记录集的排序 create table mynumber(id int,name varchar(10));
         insert into mynumber values(1,'no1');
         insert into mynumber values(2,'no2');
         insert into mynumber values(3,'no3');
        insert into mynumber values(4,'no4');
        insert into mynumber values(5,'no5');
        insert into mynumber values(5,'no6');
        insert into mynumber values(6,'no7');
        insert into mynumber values(7,'no8');
        insert into mynumber values(8,'no9');
        insert into mynumber values(9,'no10');
        insert into mynumber values(9,'no11');
        insert into mynumber values(9,'no12');
        insert into mynumber values(10,'no13');
        insert into mynumber values(10,'no14');
        insert into mynumber values(10,'no15');
        insert into mynumber values(11,'no16');
        insert into mynumber values(12,'no17');
        insert into mynumber values(13,'no18');

     select id,name,RANK() over ( order by id ) case1,
            DENSE_RANK() over ( order by id ) case2,
            row_number() over ( order by id ) case3 from mynumber;

   

(1) 取前10条不同id记录,假如最后1条记录的ID依然有相同的,那么取出来

   select id,name from mynumber where id in
         (select id from (select distinct id
          from mynumber) tt where rownum<=10);

           select * from (select id,name,RANK()
           over ( order by id ) case1,DENSE_RANK()
           over ( order by id ) case2,row_number()
            over ( order by id ) case3 from mynumber)
            tt where case1<=10;
          select * from (select id,name,RANK() over
          ( order by id ) case1,DENSE_RANK() over
          ( order by id ) case2,row_number() over
          ( order by id ) case3 from mynumber)
           tt where case1 between 5 and 10;

   (2)取前10条记录,假如第10条记录的ID 还有相同的,那么取出来

   select * from mynumber where id in
           (select id from mynumber where rownum <=10);

            select * from (select id,name,RANK()
            over ( order by id ) case1,DENSE_RANK()
            over ( order by id ) case2,row_number()
            over ( order by id ) case3 from mynumber)
            tt where case2<=10;
            select * from (select id,name,RANK()
             over ( order by id ) case1,DENSE_RANK()
            over ( order by id ) case2,row_number()
            over ( order by id ) case3 from mynumber)
            tt where case2 between 5 and 10;

(3)取前10条记录

       select id,name from mynumber
              where rownum <=10;
               select id,name from
              (select id,name,rownum rn from
                mynumber where rownum <= 10 ) where rn >= 5;

              select * from (select id,name,RANK()
              over ( order by id ) case1,DENSE_RANK()
               over ( order by id ) case2,row_number()
              over ( order by id ) case3 from mynumber)
              tt where case3<=10;
              select * from (select id,name,RANK()
              over ( order by id ) case1,DENSE_RANK()
              over ( order by id ) case2,row_number()
              over ( order by id ) case3 from mynumber)
               tt where case3 between 5 and 10;



标签:查前,insert,几条,over,order,mynumber,Oracle,id,select
From: https://blog.51cto.com/emanlee/8229587

相关文章

  • ASP.NET和Oracle连接问题的解决方法 - Unable to load DLL (oci.dll)
    以下适用于Windows2003:不少人在做ASP.NET+Oracle开发的时候都会碰到连接问题,提示“UnabletoloadDLL(oci.dll)”,但这个文件在系统中是存在的,很多人为此焦头烂额,我看到很多的帖子都是求助这个问题的。其实并非所有的人都会碰到这个问题,只在于用硬盘分区格式为NTFS的用户,既然oci.......
  • 用DELPHI 6登入ORACLE的问题Vendor initialization failed
    Vendorinitializationfailed.IFYOUUSEBDETOCONNECTTOORACLE, YOUCANTRYTHIS:1.OpenBDEAdministrator;2.GotoConfigurationTab3.OpenDrivers\Native\Oracle4.ifyouuseOracle8or8iClient,In"DLL32"Field,......
  • 安装oracle9i时碰到缺少或无效口令提示
    在即将结束安装的时候,oracle提示要输入sys密码和system密码,自己尝试了n次都不能成功,提示ora-00988:缺少或无效口令提示。在网上查找以后,发现oracle9i对密码有如下要求:系统用户(即sys,system)口令长度不能小于7个字符。并且第一个字符不能为数字。并且全部口令中需要数字+字......
  • 安装Oracle9i,遇到“File not found D: \oracle\ora92\ocs4j\admin\OCS4J.propert
    在安装Oracle9i,遇到“FilenotfoundD:\oracle\ora92\ocs4j\admin\OCS4J.properties”,点击忽略也能完成安装,但是又担心有不妥,于是四处查找答案,不过也没有看到对这个东东的解释。原因是Oracle安装版本中\DISK1\stage\Components\oracle.ocs4j\2.1.0.0.0a\1\DataFiles\Expanded......
  • Oracle 9i 启动OEM或OMS
    不过在登陆到oms之前,你必须首先安装oms服务。启动oracleuniversalinstaller,查看已安装产品,展开oracle主目录--oraHome92,如果没有oracle9imanagementandintegration,表示尚未安装。安装方法是:在oracleuniversalinstaller的可用产品列表中,选择安装oracle9imanagementandi......
  • Oracle中B-tree索引的访问方法(十一)-- 索引的分裂行为
    索引的分裂行为当某个索引块中要插入新的索引条目,但其中又没有可用空间时,就会发生索引的分裂。根据分裂发生所在的索引块类型的不同,可以分为在根块上发生的分裂,在分支块上发生的分裂和在叶子块上发生的分裂。下面,就这三种情况做分别介绍。从前面的实验中,我们已经看到,大约每个索引块......
  • 生产真实案例:震惊,几条SQL把服务器干崩了,事后还大言不惭!
    大家好,我是冰河~~今天跟大家分享一个发生在今天凌晨的真实案例,这篇文章也是我事后临时写出来的,处理事情的过程有点无语,又有点气愤!事件背景事情的背景是这样的:一个朋友今年年初新开了一家公司,自己是公司的老板,不懂啥技术,主要负责公司的战略规划和经营管理,但是他们公司的很多事情......
  • Oracle IMP-00010: 不是有效的导出文件,标题验证失败
    大概有两种情况:1.imp/exp的版本不对,也就是说低版本的导出,可以用高版本的导入,但高版本导出不能用低版本的导入,这个版本不只是指imp/exp的版本,也是指数据库的版本。也就是说,低版本的SQLDeveloper/Oracledatabase/Oracleclient导出的dmp文件,可以导入到高版本的SQLDeveloper/Orac......
  • Oracle ORA-12514:TNS:监听程序当前无法识别连接描述符中请求的服务
    oracle10g配置客户端时,测试连接出现错误(NetConfigurationAssistant--本地Net服务名配置):ORA-12514:TNS:监听程序当前无法识别连接描述符中请求的服务随后打开:D:\oracle\product\10.2.0\db_1\NETWORK\ADMIN\listener.ora 内容如下:#listener.oraNetworkConfigurationFile:D......
  • oracle 9i 10g 11g安装文件及补丁集下载地址 全版(备用)
    Oracle9iDatabaseRelease2Enterprise/Standard/PersonalEditionforWindowsNT/2000/XPhttp://download.oracle.com/otn/nt/oracle9i/9201/92010NT_Disk1.ziphttp://download.oracle.com/otn/nt/oracle9i/9201/92010NT_Disk2.ziphttp://download.oracle.com/otn/nt/orac......