How to write a group rank in a step

Source: Internet
Author: User

1. For example, there are two classes, four people, 2 students per class, 2 genius

2. Use the Row_number () function to rank two classes, such as

3. Find out the top three students of each class, just use the rank, observed rank is everyone in their class position, so in the outside package a layer in the condition of query on it can be

Attach the SQL statement:

 withCTE as(SELECT 'Class One'Class'Zhang San'Name AAchievementsUNION  All         SELECT 'Class One'Class'Zhang Si'Name -AchievementsUNION  All         SELECT 'Class Two'Class'Zhang Five'Name -AchievementsUNION  All         SELECT 'Class Two'Class'Zhang Six'Name98AchievementsUNION  All         SELECT 'Class Two'Class'Zhang Qi'Name $AchievementsUNION  All         SELECT 'Class One'Class'Zhang Eight'Name theAchievementsUNION  All         SELECT 'Class Two'Class'Nines'Name theAchievementsUNION  All         SELECT 'Class One'Class'Zhang 10'Name theresults)--SELECT * from CTE order by [class] descSelectA.*  from (SelectRow_number () Over(PARTITION by [class] Order  by [Achievements] desc) Rank,*  fromCTE) AwhereA.rank<=3 Order  byClassdesc, rankASC

How to write a group rank in a step

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.