sql >> Base de Datos >  >> RDS >> Mysql

SQL:subconsultas para el resultado superior sin orden por

Necesita tener otra subconsulta para esto,

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
                )
            )