sql >> Databasteknik >  >> RDS >> Mysql

Hitta posten med maximalt värde i SQL

WITH ClassScores AS
(
    SELECT 1 AS class, 'Anna' AS name, 34 AS score 
    UNION
    SELECT 1, 'Andy', 80  
    UNION
    SELECT 2, 'Brooke', 90  
    UNION
    SELECT 2, 'Brad', 70  
    UNION
    SELECT 3, 'Charles', 67  
    UNION
    SELECT 3, 'Christina', 66 
)

SELECT C1.Class, C1.Name, C1.Score
  FROM ClassScores AS C1
  JOIN (SELECT Class, MAX(Score) AS MaxScore
          FROM ClassScores
         GROUP BY Class
       ) AS C2
    ON C1.Class = C2.Class
   AND C1.Score = C2.MaxScore
 ORDER BY C1.Class;


  1. hur man importerar från MS Excel till MYSQL DB

  2. Lagrade procedurer med MySQL Workbench

  3. Oracle Developer Tools för visual studio .net-fel

  4. Anropa en medlemsfunktion real_escape_string() på ett icke-objekt