sql SELECT TOP,OFFSET,FETCH.sql
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql SELECT TOP,OFFSET,FETCH.sql相关的知识,希望对你有一定的参考价值。
/*****************************
TOP
*****************************/
/*
Allows you to select the first number of rows from a table.
When used with an ORDER BY clause, it'll order all of the rows and then select the TOP based on that ordering.
If you don't select ORDER BY, it'll pull the first however many rows become available (apparently this is the
the rows in order that they're physically stored, but this order isn't guaranteed without an ORDER BY clause).
TOP is a good way to sample what's in a table without pulling the entire thing, but to do
this you need to do it without an ORDER BY clause, and you can't be guaranteed which rows you'll get.
If you use an ORDER BY clause with TOP, it'll sort the entire table and then select the TOP rows,
so you don't get any cost savings if you have to scan the entire table anyway to order it.
You can think of TOP and OFFSET-FETCH as being extensions of the ORDER BY clause. Logically, they're
processed after the FROM, WHERE, GROUP, HAVING, and SELECT phases. For example, if you use grouping,
TOP and OFFSET-FETCH will happen after the grouping.
*/
-- SELECT TOP twenty rows, showing all columns (*)
SELECT TOP (20) * FROM customers1
ORDER BY CustomerName;
-- example of how to select the bottom 40 rows by using DESC at end of ORDER BY clause (it orders by lowest values first, then selects the top 10)
SELECT TOP 40 CustomerID, LastName, FirstName FROM customers1
ORDER BY CustomerID DESC;
-- SELECT TOP WITH TIES, selects top twenty rows, but shows all rows that ties for for the last place being selected. In this example below, you'll get more than 20 rows showing all those values that tie for 20th place will be shown.
SELECT TOP 20 WITH TIES * FROM customers1
ORDER BY CustomerName;
-- SELECT top five percent of entries.
SELECT TOP (5) PERCENT * FROM customers1
ORDER BY CustomerName;
/*****************************
OFFSET AND FETCH
*****************************/
/*
When ordering, OFFSET allows you to skip a certain number of rows, and then FETCH allows you to retrieve
a certain number of rows from there. OFFSET does not require a FETCH clause, but FETCH does require
an OFFSET clause. If you want to FETCH a certain number of rows but you don't want to OFFSET (you want
to start from the first result), then you can use OFFSET 0, which is the same as not OFFSETing anything.
With OFFSET, you follow by the ROWS keyword, and you can use ROWS or ROW interchangably.
With FETCH, you follow by NEXT XXX ROWS ONLY. You can alternately use FIRST XXX ROWS ONLY
*/
-- how to skip the first 10 rows using OFFSET, and pull all rows after that.
SELECT * from T_APPEAL
ORDER BY appeal_no
OFFSET 10 ROWS
-- how to skip the first 10 rows using OFFSET, and then return the next 25 rows using FETCH
SELECT * from T_APPEAL
ORDER BY appeal_no
OFFSET 10 ROWS FETCH NEXT 25 ROWS ONLY
/* how to FETCH only the first 10 rows with a 0 OFFSET. This is equivalent to using TOP (10).
FETCH is standard in SQL, whereas TOP is proprietary to SQL Server, and therefore you might want to
use FETCH. However, SQL Server allows additional capabilities like TOP PERCENT and WITH TIES.
*/
select * from T_APPEAL
ORDER BY appeal_no
OFFSET 0 ROWS FETCH NEXT 10 ROWS ONLY
/*
How to implement a paging solution, breaking the results into pages and then selecting a page.
This declares @pagesize as being 25 rows, and @pagenumber as page 3.
The OFFSET ((@pagenumber - 1) * @pagesize) ROWS FETCH NEXT @pagesize ROWS ONLY takes (3-1) = 2, then
multiplies that by 25 to get an OFFSET of 50 rows. Then it FETCHes the NEXT 25 rows since that is
the page size.
*/
DECLARE @pagesize AS INT = 25, @pagenumber AS INT = 3
SELECT * from T_APPEAL
ORDER BY appeal_no
OFFSET ((@pagenumber - 1)) * @pagesize ROWS FETCH NEXT @pagesize ROWS ONLY
以上是关于sql SELECT TOP,OFFSET,FETCH.sql的主要内容,如果未能解决你的问题,请参考以下文章
在使用limit的offset比较大的时候,使用子查询优化sql,减少回表操作
SQL SERVER语句提示错误 delete top (5) FROM (SELECT top 100 percent * FROM 表名 ORDER BY DATE0) a