mysqldude

Running totals/determine row position

Version compatibility

StepMySQLMariaDB
5.65.78.08.4910.610.1111.411.8
Row position with @position := @position + 1 (original)
original post
✓✓⚠ 1287⚠ 1287⚠ 1287✓✓✓✓
Running total with @total over derived table (original)
original post
✓✓⚠ 1287⚠ 1287⚠ 1287✓✓✓✓
Row position with ROW_NUMBER() OVER (modern)
modern rewrite
✗ 1064✗ 1064✓✓✓✓✓✓✓
Running total with SUM() OVER (modern)
modern rewrite
✗ 1064✗ 1064✓✓✓✓✓✓✓
Row position with @position := @position + 1 (original) original post
MySQL 5.6✓5.7✓8.0⚠ 12878.4⚠ 12879⚠ 1287
MariaDB 10.6✓10.11✓11.4✓11.8✓
Running total with @total over derived table (original) original post
MySQL 5.6✓5.7✓8.0⚠ 12878.4⚠ 12879⚠ 1287
MariaDB 10.6✓10.11✓11.4✓11.8✓
Row position with ROW_NUMBER() OVER (modern) modern rewrite
MySQL 5.6✗ 10645.7✗ 10648.0✓8.4✓9✓
MariaDB 10.6✓10.11✓11.4✓11.8✓
Running total with SUM() OVER (modern) 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-23 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 (4)
  • Row position with @position := @position + 1 (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
  • Running total with @total over derived table (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
  • Row position with ROW_NUMBER() OVER (modern) (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 event_id, money DESC) AS position F
  • Running total with SUM() OVER (modern) (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 event_id DESC) AS running_total FRO

The original article

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

This post demonstrates how to get running totals of row positions on a query. Firstly let us look at a query that permits us to order entries and then give them a position starting from 1 to the number of entries in the returned query.

To give a unique ordered identifier we will use user session variables @var. These are maintain within the scope of the user session only and are particularly good at keeping track of field values between queries and also within queries as we shall soon see.

We have a table that contains an event_id and prize money fields. We want to order the information by event_id and by prize money descending. We also want to introduce a position field which contains the position of the row within the returned query set of rows.

mysql> select money, event_id from prize order by event_id, money desc;
+-------+----------+
| money | event_id |
+-------+----------+
|   120 |      101 |
|    60 |      101 |
|    30 |      101 |
|    10 |      102 |
|     5 |      102 |
|     2 |      102 |
|   100 |      103 |
|    60 |      103 |
|    40 |      103 |
|  1000 |      401 |
+-------+----------+
10 rows in set (0.00 sec)

From this we have correctly ordered the rows but we are missing the position field. In this case we need to set the user session variable and this will be used within our query as follows:

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

mysql> select money, event_id, @position:=@position+1 as position from prize order by event_id, money desc;
+-------+----------+----------+
| money | event_id | position |
+-------+----------+----------+
|   120 |      101 |        1 |
|    60 |      101 |        2 |
|    30 |      101 |        3 |
|    10 |      102 |        4 |
|     5 |      102 |        5 |
|     2 |      102 |        6 |
|   100 |      103 |        7 |
|    60 |      103 |        8 |
|    40 |      103 |        9 |
|  1000 |      401 |       10 |
+-------+----------+----------+
10 rows in set (0.00 sec)

Be aware that if you re-run the query you need to reset the value of @position back to 0 otherwise the next number returned will be 11 in this case.

mysql> select money, event_id, @position:=@position+1 as position from prize order by event_id, money desc;
+-------+----------+----------+
| money | event_id | position |
+-------+----------+----------+
|   120 |      101 |       11 |
|    60 |      101 |       12 |
|    30 |      101 |       13 |
|    10 |      102 |       14 |
|     5 |      102 |       15 |
|     2 |      102 |       16 |
|   100 |      103 |       17 |
|    60 |      103 |       18 |
|    40 |      103 |       19 |
|  1000 |      401 |       20 |
+-------+----------+----------+
10 rows in set (0.00 sec)

So far so good. But what happens if we want to keep a running total of the prize money group by event_id. In other words we want to show the event_id, the sum of the prize money at that event and a running total. You may think that this is done as follows:

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

mysql> select event_id, sum(money) as money, @total := @total + sum(money) from prize group by event_id order by event_id asc;
+----------+-------+-------------------------------+
| event_id | money | @total := @total + sum(money) |
+----------+-------+-------------------------------+
|      101 |   210 |                           210 |
|      102 |    17 |                            17 |
|      103 |   200 |                           200 |
|      401 |  1000 |                          1000 |
+----------+-------+-------------------------------+
4 rows in set (0.00 sec)

This is not the correct result, we should be expecting the entries to be added. However, this feature does not work with aggregate functions such as sum. In this case here what we need to do is create a subquery to return the individual rows and then we track the running totals based on that.

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

mysql> select event_id, summoney, @total:=@total+summoney
    -> from (select event_id, sum(money) summoney from prize group by event_id)
    -> tot
    -> order by event_id desc;
+----------+----------+-------------------------+
| event_id | summoney | @total:=@total+summoney |
+----------+----------+-------------------------+
|      401 |     1000 |                    1000 |
|      103 |      200 |                    1200 |
|      102 |       17 |                    1217 |
|      101 |      210 |                    1427 |
+----------+----------+-------------------------+
4 rows in set (0.01 sec)

That completes this mini tutorial. Keep following us on twitter, facebook or at this site.