Assuming I have the following:
tableA
id (auto increment primary key) | age | country
1 | 5 | Germany
2 | 6 | Spain
3 | 7 | France
4 | 7 | Spain
5 | 8 | France
6 | 9 | France
7 | 2 | Mexico
8 | 7 | Saudi Arabia
9 | 5 | Saudi Arabia
I want the resulting table:
tableA
id (auto increment primary key) | age | country | country_number
1 | 5 | Germany | 1
2 | 6 | Spain | 2
3 | 7 | France | 3
4 | 7 | Spain | 2
5 | 8 | France | 3
6 | 9 | France | 3
7 | 2 | Mexico | 4
8 | 7 | Saudi Arabia | 5
9 | 5 | Saudi Arabia | 5
Note that “country_number” just a unique number based on whatever a unique country name is.
Using a cursor is slow. How do I make it such that I can do some sort of “group by”? to assign a unique identifier per country? Or is group by not necessary?
query could be (you won’t have 5 for Saudi Arabia, but at least distinct country_number for each country)
SqlFiddle
To do things properly,
see SqlFiddle