T-sql:top and Offset-fetch Screening (v)

Source: Internet
Author: User

The data can be filtered by were and having conditions, so how do you filter the data by sorting it?

1.TOP filtering

Used to limit the number of rows returned by a query or the percentage of rows.

For example, we filter the order form to the most recent order 5

SELECT TOP (5) OrderID, OrderDate, CustID, Empid  from sales.orders ORDER  by DESC;

This is filtered by sorting the data

The ORDER BY clause is computed after the SELECT clause, and the Select contains the DISINCT option, so the DINSINCT calculation order is greater than top when the data is repeated before the top calculation

It should be noted that when a SQL statement appears top and order by when order by is calculated two times first the data is displayed sorted the second time the filter is defined for top.

I don't understand. Is the top query returning a table result or a cursor? , typically a query with an ORDER BY clause returns a cursor rather than a relationship result.

Add the Percent keyword to the top option to find the most recently generated 1% orders

SELECT TOP (1) PERCENT OrderID, OrderDate, CustID, Empidfrom Sales.ordersorder by OrderDate DESC;

Multiple lines with the same order date as the Sort field appears with the same date each execution has uncertainties that return a different result, all to ensure the uniqueness of the sort field.

The problem is, what if the problem already exists and does not ensure uniqueness? We can join with the TLEs option primary function returns all results of the last match row in the table to prevent inconsistencies in the returned data because of the row origin.

SELECT TOP (5 with TIES OrderID, OrderDate, CustID, Empid from sales.ordersORDER  byDESC;

Note: Even if the ORDER BY clause top option is not sorted by the sort is ambiguous, SQL Server returns the first physical access to n rows of data instead of according to the table order.

2.offset-fetch filtering

You can think of the enhanced TOP feature Offset-fetch clause as part of the ORDER BY clause how many rows the OFFSET clause skips, and how many rows the FETCH clause shows

SELECT OrderID, OrderDate, CustID, Empid  from sales.orders ORDER  by  FETCHNEXTonly;

Above is skip 50 lines show 25 rows

The Offset-fetch clause must be behind orderd by as long as there is fethch but no FETCH is possible.

The disadvantage is that the percent and with TIES options are not supported and top support

Recommended if you use only the filter rows feature, use Offset-fetch because it is standard SQL and top is not recommended unless you want the percent and with TIES options to simply filter the number of rows without the top

T-sql:top and Offset-fetch filter (v)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.