New sort function for "T-SQL Series"

Source: Internet
Author: User

Original: "T-SQL Series" New sort function

such as: Row_number, RANK, Dense_rank
All three analysis functions are sorted from 1 in the col1 group

Row_number () is a sort that does not have a duplicate value (even if two days of record equality are not duplicated), it can be used to achieve paging
Dense_rank () is a sequential sort, with two second names still following the third place
RANK () is a jump shoot, two second place is fourth place

Example:

DECLARE @t1 TABLE(SequenceINT, NameVARCHAR( -), scoreINT    )INSERT   into @t1        SELECT  1 ,                'Armor' ,                7        UNION  All        SELECT  2 ,                'Armor' ,                8        UNION  All        SELECT  3 ,                'Armor' ,                8        UNION  All        SELECT  4 ,                'Armor' ,                8        UNION  All        SELECT  5 ,                'Armor' ,                9        UNION  All        SELECT  1 ,                'b' ,                Ten        UNION  All        SELECT  2 ,                'b' ,                6        UNION  All        SELECT  3 ,                'b' ,                Ten        UNION  All        SELECT  4 ,                'b' ,                6        UNION  All        SELECT  5 ,                'b' ,                8SELECTSequence, Name, Score, row_number () Over(ORDER  bySequence, score) asRowNumber1, row_number () Over(ORDER  bySequence, ScoreDESC) asRowNumber2, RANK () Over(ORDER  byScore) asRnk, Dense_rank () Over(ORDER  byScore) asdensernk, NTILE ( the) Over(ORDER  byScore) asBuckets from    @t1

Result set:

New sort function for "T-SQL Series"

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.