Mysql
 sql >> Base de Dados >  >> RDS >> Mysql

SQL - subconsultas para o resultado principal sem ordem por


Você precisa ter outra subconsulta para isso,
SELECT c.Givenname, c.Familyname, COUNT(r.places) AS TotalPlaces
FROM Competitors c 
INNER JOIN Results r ON r.Competitornum = c.Competitornum
WHERE r.place IN (1,2,3)
GROUP BY c.Givenname, c.Familyname
HAVING COUNT(r.places) = 
            (
                SELECT MAX(TotalPlaces)
                FROM
                (
                    SELECT COUNT(g.places) AS TotalPlaces
                    FROM Competitors f 
                    INNER JOIN Results g ON f.Competitornum = g.Competitornum
                    WHERE g.place IN (1,2,3)
                    GROUP BY f.Givenname, f.Familyname
                )
            )