mysqldude

Counting repetitions in a specific order

Version compatibility

StepMySQLMariaDB
5.65.78.08.4910.610.1111.411.8
Number the runs with @x / @data (original)
original post
✓✓⚠ 1287⚠ 1287⚠ 1287✓✓✓✓
Run length and number of runs via nested derived tables (original)
original post
✓✓⚠ 1287⚠ 1287⚠ 1287✓✓✓✓
Gaps-and-islands with ROW_NUMBER() (MySQL 8.0+, MariaDB 10.2+)
modern rewrite
✗ 1064✗ 1064✓✓✓✓✓✓✓
Number the runs with @x / @data (original) original post
MySQL 5.6✓5.7✓8.0⚠ 12878.4⚠ 12879⚠ 1287
MariaDB 10.6✓10.11✓11.4✓11.8✓
Run length and number of runs via nested derived tables (original) original post
MySQL 5.6✓5.7✓8.0⚠ 12878.4⚠ 12879⚠ 1287
MariaDB 10.6✓10.11✓11.4✓11.8✓
Gaps-and-islands with ROW_NUMBER() (MySQL 8.0+, MariaDB 10.2+) modern rewrite
MySQL 5.6✗ 10645.7✗ 10648.0✓8.4✓9✓
MariaDB 10.6✓10.11✓11.4✓11.8✓
✓works ✓ 1062fails with the error the step demonstrates ⚠works with a warning ≠runs, result differs ✗fails Tested 2026-11-25 on 5.6.51, 5.7.44, 8.0.46, 8.4.13, 9.7.4, 10.6.28, 10.11.19, 11.4.13, 11.8.9
What the warnings and failures say (3)
  • Number the runs with @x / @data (original) (MySQL 8.0, MySQL 8.4, MySQL 9): warning 1287: Setting user variables within expressions is deprecated and will be removed in a future release. Consider alternatives: 'SET variable=expres
  • Run length and number of runs via nested derived tables (original) (MySQL 8.0, MySQL 8.4, MySQL 9): warning 1287: Setting user variables within expressions is deprecated and will be removed in a future release. Consider alternatives: 'SET variable=expres
  • Gaps-and-islands with ROW_NUMBER() (MySQL 8.0+, MariaDB 10.2+) (MySQL 5.6, MySQL 5.7): error 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(ORDER BY id) - ROW_NUMBER() OVER (PARTITION

Related errors: 1287

The original article

First published 2011-05-04. The text below is the original; the table above shows how it behaves on current versions.

I was asked how to count the number of repetitions in a set of ordered data. For example, let us imagine we have the following set of information:

xx
xx
xx
yy
yy
xx
xx
xx
yy
yy
xy
yy
yy

What we want to do is first summarize this into:

xx 3
yy 2
xx 3
yy 2
xy 1
yy 2

To add more complexity we also want to count the number of iterations of the repetitions. In other words, this set of information should be further summarized into:

xx 3 - 2
yy 2 - 3
xy 1 - 1

In order to solve this we are going to use MySQL user session variables to keep track of the identical data values.

mysql> set @x := 0;
Query OK, 0 rows affected (0.00 sec)

mysql> set @data := "";
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT data,
    ->        IF(@data = data, @x, @x := @x + 1)  AS num,
    ->        IF(@data = data, "", @data := data) AS info
    -> FROM   data
    -> ORDER  BY id;
+------+------+------+
| data | num  | info |
+------+------+------+
| xx   |    1 | xx   |
| xx   |    1 |      |
| xx   |    1 |      |
| yy   |    2 | yy   |
| yy   |    2 |      |
| xx   |    3 | xx   |
| xx   |    3 |      |
| xx   |    3 |      |
| yy   |    4 | yy   |
| yy   |    4 |      |
| xy   |    5 | xy   |
| yy   |    6 | yy   |
| yy   |    6 |      |
+------+------+------+
13 rows in set (0.00 sec)

Now we have a set of data with which we can work with, the combination of the first and second field uniquely identify each iterations set. So by counting this information, we have found the summary of the iterations.

mysql> SELECT data,
    ->        COUNT(num)
    -> FROM   (SELECT data,
    ->                IF(@data = data, @x, @x := @x + 1)  AS num,
    ->                IF(@data = data, "", @data := data) AS info
    ->         FROM   data
    ->         ORDER  BY id) x
    -> GROUP  BY data,
    ->           num;
+------+------------+
| data | COUNT(num) |
+------+------------+
| xx   |          3 |
| xx   |          3 |
| xy   |          1 |
| yy   |          2 |
| yy   |          2 |
| yy   |          2 |
+------+------------+
6 rows in set (0.00 sec)

Now the final part to the puzzle is summarizing each of these to show each repetition:

mysql> SELECT data,
    ->        countnum,
    ->        COUNT(1) AS iterations
    -> FROM   (SELECT data,
    ->                COUNT(num) AS countnum
    ->         FROM   (SELECT data,
    ->                        IF(@data = data, @x, @x := @x + 1)  AS num,
    ->                        IF(@data = data, "", @data := data) AS info
    ->                 FROM   data
    ->                 ORDER  BY id) x
    ->         GROUP  BY data,
    ->                   num) y
    -> GROUP  BY data,
    ->           countnum;
+------+----------+------------+
| data | countnum | iterations |
+------+----------+------------+
| xx   |        3 |          2 |
| xy   |        1 |          1 |
| yy   |        2 |          3 |
+------+----------+------------+
3 rows in set (0.00 sec)