SQL >> Base de Datos >  >> RDS >> Mysql

Cómo agrupar apariciones consecutivas por equipo en MySQL

En MySQL 8 puedes usar funciones de ventana para detectar rachas consecutivas del mismo equipo:

WITH x AS ( SELECT *, ROW_NUMBER() OVER (ORDER BY year) - ROW_NUMBER() OVER (PARTITION BY team ORDER BY year) AS grp FROM results ) SELECT team, MIN(year) AS start_year, MAX(year) AS end_year, COUNT(*) AS total FROM x GROUP BY team, grp;

La diferencia entre numeraciones identifica cada bloque consecutivo.