In Redshift I frequently use the row_number()
function. However the version of MySQL our DB uses doesn't support this.
To work around it can use:
SELECT a.i, a.j, count(*) as row_number FROM test a
JOIN test b ON a.i = b.i AND a.j >= b.j
GROUP BY a.i, a.j