How SQL Server groups numbers

Source: Internet
Author: User

We often have this requirement in our production practice: Group numbering.

There is a City area table region as follows:

We need to group the above table region by city and sort the region to get the following result:

The specific SQL is as follows:

1 Select  2Right ('+'+row_number ()over by  order by region),2as3 from Region

This method mainly uses the over function by grouping the city, then sorting the region (by default alphabetical alphabetical order), and finally get the result we want.

Of course, there is another way to do the same:

Select City,region,region_no =right (+ +(selectCount(1)+1fromwhere City= and Region<a.region),2) from Region A

If other small partners have better ideas, welcome to the bottom of the message, we learn from each other, common progress, thank you!

How SQL Server groups numbers

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.