[Database] basic usage of the ROW_NUMBER () OVER function, row_numberover

Source: Internet
Author: User

[Database] basic usage of the ROW_NUMBER () OVER function, row_numberover

(1) ROW_NUMBER () OVER (order by column)

Generally, developers and administrators use temporary table and column-related subqueries in a query to calculate and generate row numbers. SQL Server 2005 now provides a function to generate row numbers instead of all redundant code.

To put it simply, row_number () starts from 1 and returns a number for each group record. ROW_NUMBER () OVER (order by xlh DESC) first orders the CourseID column in descending ORDER, return a sequence number for all xlh records after descending order.

Select ROW_NUMBER () over (order by CourseID desc) as RowIndex, * from TeacherRoles where ClassCode = '20140901'




Usage of over () and row_number () functions in SQL

Select row_number () over (order by xxxx) from xxxx;

It is found that the sequence sorted by xxxx starts from 1.

How to Use the row_number () over Function

In short, row_number () starts from 1 and returns a number for each group record. ROW_NUMBER () OVER (order by xlh DESC) first orders the xlh column in descending ORDER, return a sequence number for no xlh records after descending order. You spell the SQL statement in the program, so it is a "string" + "string.
Example:
Xlh row_num
1700 1
1500 2
1085 3
710 4
680 5
If you still don't understand it, ask me again.

Related Article

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.