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. Centos下搭建ftp服务器

    完全不用那么麻烦,直接可以用xshell中自带的传输文件功能,下载客户端xftp安装就行,不用配置,可以在windows系统向Linux系统的任何文件地方上传下载文件,简单方便,大大节约时间, vsf ...

  2. php 循环向<select>添加选项

    在控制器内:$this->assign('data',$data);

  3. [ActionScript 3.0] AS3 3D双圆环贴图

    package { import flash.display.Bitmap; import flash.display.BitmapData; import flash.display.MovieCl ...

  4. 使用 Heka 导入自定义的nginx日志到Elasticsearch

    重置Heka执行进度 heka的进度配置文件存在配置项 base_dir 设置的目录,只需要删除这个文件夹下面的内容,就可以完全重置heka的进度. base_dir 配置项默认是在下面目录: '/v ...

  5. Sqlserver中存储过程,触发器,自定义函数

    Sqlserver中存储过程,触发器,自定义函数: 1. 触发器:是针对数据库表或数据库的特殊存储过程,在某些行为发生的时候就会被激活 触发器的分类: DML触发器:发生在数据操作语言执行时触发执行的 ...

  6. dedecms首页怎么调用公司简介的内容

    DeDeCMS功能虽然强大,但还是有些细节上的功能没有实现,正如本文描述的问题一样,DEDECMS要在网站首页调用公司简介的内容,而且还要截取前多少个字符数的时候,DEDECMS标签中没有能实现这样的 ...

  7. 初探appium之环境搭建

    前段时间一直在折腾python的爬虫,想搞接口自动化.但是写了一个月,发现在我现在的这份的工作中根本接触不到接口.所以就想先放下来,先做点目前能够接触到的,也需要做的东西. 东西越来越多,人手不足.自 ...

  8. jmeter随笔(1)-在csv中数据为json格式的数据不完整

    昨天同事在使用jmeter遇到问题,在csv中数据为json格式的数据,在jmeter中无法完整的取值,小怪我看了下,给出解决办法,其实很简单,我们一起看看,看完了记得分享给你的朋友. 问题现象: 1 ...

  9. 利用canvas实现的中点Bresenham算法

    Bresenham提出的直线生成算法的基本原理是,每次在最大位移方向上走一步,而另一个方向是走步还是不走步取决于误差项的判别,具体的实现过程大家可以去问度娘.我主要是利用canvas画布技术实现了这个 ...

  10. Flash图表控件FusionCharts如何自定义图表上的垂直线

    什么是垂直分割线 垂直(或条形图中的水平)分隔线是用来帮助用户分隔数据块的.可以被放置在任何两个数据点,即使是不规则的间隔也可以. <chart caption='Monthly Revenue ...