I have a table in Sql Sever that looks like this,
Br Type Sales
102 A 20
102 B 10
102 D 8
102 F 12
103 A 18
103 C 7
103 D 15
103 E 3
I want to create a new table in SQL Server that adds a column to the existing table above that provides a cumulative count for each unique Br column and sorts the data Ascending according to the sales column.
The new table should look like this,
BrCount Br Type Sales
1 102 D 8
2 102 B 10
3 102 F 12
4 102 A 20
1 103 E 3
2 103 C 7
3 103 D 15
4 103 A 18
I am not sure where to start. I thought about using some kind of loop that analyse at each iteration what Br number a row contain and update the BrCount column with the previous value + 1 or just a 1 if the Br number from the previous row does not match, but i struggle to find examples on such kind of loops.