An interesting lookup - Search the ID number of the maximum value (transferred from: http:blog.9cbs.netdhlhh)

xiaoxiao2021-03-06  103

My friend has a problem, each has a game, records the first score, after dozens of games, the results are as follows: ID Name Score1 A 882 B 763 C 664 C 905 B 776 A 567 B 77 8 C 679 A 44 ... Of course, there are still many best results, and the best grades are the number ID number, which is the best grade, is like: ID Name score1 a 885 B 774 C 90 I felt like a primary question, put the eyes on the maximum, but then I feel that this focus is on the maximum value, but the maximum value of the ID number. Difficulties: Search for the ID number of the maximum value: Everyone may have a few largest value brain: start thinking about this MAX function: SELECT MAX (Score), [name] group by [name] But this damn How can I also insert a first-line hopes: use a child: SELECT [ID], [Name], Scorefrom Table1 Awhere SCORE IN

(Select Max (B.score) AS MaxScore

From Table1 B

Group by b. [Name]), this ID number is still more, the reason

Since the search is based on the set of largest values, such as 77 this maximum, it may be B's maximum, but A and C may also have 77 this number, but not the maximum, so ...... Victory, finally added a condition, make the Name value in the maximum collection, compliant with the search Name value, [name], scorefrom table1 awhere score in

(Select Max (B.score) AS MaxScore

From Table1 B

GROUP BY b. [Name] Having a. [Name] = b. [Name]) The result is successful -------------------------------------------------------------------------------------------------------------------------- ----- or: SELECT T.NAME, T.SCORE, MIN (T.ID) AS ID from Table1 T. Name = x.name and t.score = x.score group by t.name, t.score

转载请注明原文地址:https://www.9cbs.com/read-103640.html

New Post(0)