Skip to content
Advertisement

How to incrementally count the number of repeated instances

I want to incrementally count the number of repeated instances in a table, so if I have a table like this:

Using the column “name” as reference, the output would be

The DBMS implementation is MySQL, version 5.6

Advertisement

Answer

In MySQL 5.x, where window functions are not available, one option uses a correlated subquery:

You could also do this with user variables:

There are pros and cons to both approaches. The second solution scales better than the first against a large dataset, however user variables are tricky in MySQL, and are now officially planned for deprecation in a future version.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement