From time to time you may have the need to grab the top X number of rows returned from a table. For instance “give me the 10 vendors we pay the most from the voucher table”.

This might seem impossible in something like query as there is no way to stop the output after 10 rows… unless you use the SQL below that will let you do just that:

SELECT *FROM (SELECT VENDOR_ID, COUNT(*) FROM PS_VOUCHER GROUP BY VENDOR_ID ORDER BY 2 DESC)WHERE ROWNUM <= 10

What this will do is run the inner SQL statement (a view that acts like a table) that will grab vendor counts from the voucher table and order it by the count descending. Then the outer SQL statement will only show the first 10 rows returned.

If you needed a larger number simply increase the 10 to what ever count you need. This particular SQL can be slow since it is doing a full table scan of the voucher table.

The return from this table can also be joined to other tables for a report.  (This example would need more fine-tuning to pull the single row desired from PS_VENDOR_ADDR, but for simplicity the correct SQL has not been added.)

SELECT V.CNT, AD.*FROM PS_VENDOR_ADDR AD, (SELECT VENDOR_ID, COUNT(*) AS CNT FROM PS_VOUCHER GROUP BY VENDOR_ID ORDER BY 2 DESC) VWHERE ROWNUM <= 10 AND AD.VENDOR_ID = V.VENDOR_ID

Retrieve Only First x Rows的更多相关文章

  1. Hibernate--------八大类HQL查询集合

    Hibernate的   八大类HQL查询集合 Hibernate的八大类HQL查询集合: 一:属性查询(SimplePropertyQuery) 1,单一属性查询 *返回结果集属性列表,元素类型和实 ...

  2. 【Android Api 翻译4】android api 完整翻译之Contacts Provider (学习安卓必知的api,中英文对照)

    Contacts Provider 电话簿(注:联系人,联络人.通信录)提供者 ------------------------------- QUICKVIEW 快速概览 * Android's r ...

  3. Laravel Quickstart

    Installation Via Laravel Installer First, download the Laravel installer using Composer. composer gl ...

  4. SQL Fetch size

    JDBC performance tuning with optimal fetch size February 1, 2009 31 Comments Tuning performance usin ...

  5. Intel daal数据预处理

    https://software.intel.com/en-us/daal-programming-guide-datasource-featureextraction-py # file: data ...

  6. Oracle Flashback Transaction Query with Oracle Flashback Version Query

    Oracle Flashback Transaction Query with Oracle Flashback Version Query In this example, a database a ...

  7. DBCC CHECKDB 遭遇Operating system error 112(failed to retrieve text for this error. Reason: 15105) encountered

    我们一个SQL Server服务器在执行YourSQLDBa的作业YourSQLDba_FullBackups_And_Maintenance时遇到了错误: Exec YourSQLDba.Maint ...

  8. CentOS系统yum源使用报错:Error: Cannot retrieve repository metadata (repomd.xml) for repository: rpmforge.

    服务器上的yum突然不好使用,使用yum的时候报错如下:[root@bastion-IDC src]# yum list......Could not retrieve mirrorlist http ...

  9. Write on ……… failed: 112(failed to retrieve text for this error. Reason: 15105)

    早上检查数据库的备份邮件时,发现一台Microsoft SQL Server 2008 R2 (SP2)数据库的Maintenance Report有错误 在SSMS里面执行Exec YourSQLD ...

随机推荐

  1. C语言位运算符及作用:与、或、异或、取反、左移和右移

    一.& 按位与 如果两个相应的二进制位都为1,则该位的结果值为1,否则为0应用:(1)清零 若想对一个存储单元清零,即使其全部二进制位为0,只要找一个二进制数,其中各个位符合一下条件:原来的数 ...

  2. Dynamics CRM 2011 权限管理(转)

    http://www.cnblogs.com/LeoTang/p/3344265.html Dynamics CRM 2011 权限管理 CRM系统基于角色的权限主要通过部门.角色.用户.团队来 进行 ...

  3. angularJs的ui-router总结

    一:跑通ui-router. ui-router源码在最后面 跑通后的样子: 这个不解释了,都是很基本的东西. 二:切换视图: 这里的name可以不写,但是你得放到state的第一个参数里. 跑起来后 ...

  4. 小知识:如何解压cpio.gz文件

    第一种方法: zcat xxxx.cpio.gz | cpio -idmv 第二种方法 :第一步: gunzip xxxx.cpio.gz第二步:cpio -idmv < xxxx.cpio # ...

  5. HashCode equals

    HashCode: hashcode就是一个签名.当两个对象的hashcode一样时,两个对象就有可能一样.如果不一样的话两个对象就肯定不一样.一般用hashcode来进行比较两个东西是不是一样的,可 ...

  6. 解决删除/升级Python导致Ubuuntu无法进入桌面的问题

    找到问题的原因后于是换个思路,想大概修复了python,Ubuntu进入桌面应该也就没啥问题了.于是重新安装Python发现还是无济于事.也通过/usr/bin/python: can't find ...

  7. spring的框架集,简化的编程模型

    spring batch spring intergration spring ws spring jms spring-mvc -> spring-rest resthate z

  8. SHARED_POOL_RESERVED_SIZE参数的设置及作用 -ZHUANZAI

    还有一个参数是需要提及的:shared_pool_reserved_size.该参数指定了保留的共享池空间,用于满足将来的大的连续的共享池空间请求.当共享池出现过多碎片,请求大块空间会导致Oracle ...

  9. jxl的API

    jxl的API 使用Windows操作系统的朋友对Excel(电子表格)一定不会陌生,但是要使用Java语言来操纵Excel文件并不是一件容易的事.在Web应用日益盛行的今天,通过Web来操作Exce ...

  10. app与服务器对接

    如何做ios的app与服务器的数据传输