SQL Server window function over usage

Source: Internet
Author: User
I personally think that the introduction of window function Over after SQL Server 2005 is a pioneering work, which simplifies many complicated problems. Since it is a usage, I will not explain anything else. Just give some examples: createtableOverTableTest (idintidentity (), -- IDValint, -- value typint, -- type) godecl

I personally think that the introduction of window function Over after SQL Server 2005 is a pioneering work, which simplifies many complicated problems. Since it is a usage, I will not explain anything else. Just give some examples: create table OverTableTest (id int identity (), -- IDVal int, -- value typ int, -- type) godecl

I personally think that SQL Server launched after 2005WindowFunctionOver is a completely pioneering task, which simplifies many complicated problems.

Since YesUsageI will not explain anything else. Just give some examples:

Create table OverTableTest (id int identity (1, 1), -- IDVal int, -- value typ int, -- type) godeclare @ cou intset @ cou = 1 while @ cou <50 begininsert into OverTableTest select @ cou, @ cou % 5 + 1 set @ cou = @ cou + 1 endgoselect * from OverTableTest/* we usually use the following scenario: Paging (row_Number) Example :*/; with mycte as (select row = ROW_NUMBER () over (order by id desc), * fromOverTableTest) select * from mycte where row BETWEEN 10 and 20/* in this case, however, I have never asked the question about how Over is used. * // The application environment 2 groups obtain the first few of them, as if they were taking the test. To obtain the top 3 students in each subject: */; with mycte as (select row = ROW_NUMBER () over (partition by typ order by val desc), * fromOverTableTest) select * from mycte where row <= 3/* In this example, the partition by clause is used for grouping statistics. Let's proceed with the Third example: Get the number of classes by category, sort by the maximum number of classes */select Cunt = count (0) over (partition by typ), * fromOverTableTest order by Cunt desc, typ, id -------------------------------------------------------------------/* The example is mentioned here today. You can copy the running result and execute it directly */

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.