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. Mybatis where 1=1 和 <where>标签

    <select id="selSampleListByIDX4" resultMap="BaseResultMap" parameterType=&quo ...

  2. maven项目导入eclipse

    maven项目的配置文件 web java文件都在src下面 src/main/java src/main/webapp src/main/webapp/web-inf 导入后要重新添加jar包,设置 ...

  3. [Java] 读写字节数据,过滤流DataOutputStream和DataInputStream

    package test.stream; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io ...

  4. 使用go tool pprof分析内存泄漏、CPU消耗

    go中提供了pprof包来做代码的性能监控,在两个地方有包: net/http/pprof runtime/pprof 其实net/http/pprof中只是使用runtime/pprof包来进行封装 ...

  5. [POJ 2063] Investment (动态规划)

    题目链接:http://poj.org/problem?id=2063 题意:银行每年提供d种债券,每种债券需要付出p[i]块钱,然后一年的收入是v[i],到期后我们把本金+收入取出来作为下一年度本金 ...

  6. HDU 4786 Fibonacci Tree

    Fibonacci Tree Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) P ...

  7. lua绑定c++的时候常见得错误

    1 Error parsing reanslation unit 这种情况,首先来说你的python 2.7以及他的插件安装是完整的,最可能的原因就是自己写的ini文件,header路径错误,可以把这 ...

  8. linux包转发开发

    28号晚上接到这个任务的, 看了点epoll, 29号上午安装Ubuntu 12.10的G++, 开始把内网的vm虚拟机文件, 复制到外网, 重新建立一个虚拟机再更新, 最后外网也没能安装得了g++. ...

  9. Oracle中MD5+Base64加密实现

    JAVA实现: public static String getMD5(String str) throws Exception {   MessageDigest md5 = MessageDige ...

  10. [原创] IIS7下顶级域名301跳转到WWW域名

    百度搜索了众多方法,居然没有一个全面的IIS7下301域名跳转能用的教程,最终自己研究出了个可以用的供大家参考.1.绑定域名01ruodian.cn www.01ruodian.cn到空间: 2.在I ...