An interesting lookup--search for the ID number of the maximum value

Source: Internet
Author: User
Max value friend out of a problem, each has a,b,c three people to do the game, record the first score, after dozens of games, the results are as follows:

ID Name Score
1 a 88
2 B 76
3 C 66

4 C 90
5 B 77
6 a 56

7 B 77
8 C 67
9 a 44

......

Of course, there's plenty.

Ask for the best results for each of the three a,b,c, and list the number of the best grades, the ID number.
Such as:
ID Name Score
1 a 88
5 B 77
4 C 90

At first I felt like a beginner's problem and looked at the maximum, but then I felt that the emphasis was not on the maximum, but on the ID number of the maximum value.
Topic Difficulty: Search out the ID number where the maximum value is
Exception: Each person may appear with several of the largest values

Brain: Start thinking about the Max function first:
SELECT MAX (Score), [name] GROUP by [name]
But the damn ID number can't be inserted.

Glimmer of hope: using a subquery:
SELECT [Id],[name],score
From Table1 A
WHERE Score in
(SELECT MAX (b.score) as Maxscore
From Table1 b
GROUP by B.[name])
However, this ID number is still more out, the reason is because the search is based on the maximum set of cooperation, such as the maximum value of 77, may be the maximum of B, but A and C may also appear 77 this number, but not the maximum, so ...

Victory, and finally added a condition to make the name value in the maximum collection match the name value of the search
SELECT [Id],[name],score
From Table1 A
WHERE Score in
(SELECT MAX (b.score) as Maxscore
From Table1 b
GROUP by B.[name] (having a.[name]=b.[name])

The result was smooth.
tend to be obscure problems that are very brain


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.