I have a table with two columns.
+------+------+
| data | num |
+------+------+
| a | |
| a | |
| a | |
| b | |
| b |
Does the data have to stay in the order shown, or can we sort by the 'data' value?
If you can sort, then you only have to keep track of the current 'data' value, which can be done with variables:
set @last_data = null;
set @count = 0;
select data, @count,
case when @last_data is null or data != @last_data then @count := 1 else @count := @count + 1 end as new_count,
@last_data := data, @count
from t20120917
order by data;
If not, I think it'll be more complex...