sql >> Database teknologi >  >> RDS >> Sqlserver

sql grupper efter kun rækker, der er i rækkefølge

Dette er kendt som "øer"-problemet. Bruger Itzik Ben Gans tilgang:

;WITH YourTable AS ( SELECT 1 AS N, 'A' AS C UNION ALL SELECT 2 AS N, 'A' AS C UNION ALL SELECT 3 AS N, 'A' AS C UNION ALL SELECT 4 AS N, 'B' AS C UNION ALL SELECT 5 AS N, 'B' AS C UNION ALL SELECT 6 AS N, 'B' AS C UNION ALL SELECT 7 AS N, 'A' AS C UNION ALL SELECT 8 AS N, 'A' AS C ), T AS (SELECT N, C, DENSE_RANK() OVER (ORDER BY N) - DENSE_RANK() OVER (PARTITION BY C ORDER BY N) AS Grp FROM YourTable) SELECT COUNT(*), C FROM T GROUP BY C, Grp ORDER BY MIN(N)

  1. Monitoring Galera Cluster for MySQL eller MariaDB - Understanding Metrics (Opdateret)

  2. Dato/tidsstempel for at registrere, hvornår en post blev tilføjet til tabellen?

  3. Hvordan kan jeg bruge PDO til at hente et resultatarray i PHP?

  4. Kom godt i gang med Django-kanaler