I am aware of the statement SELECT ROW_NUMBER() OVER (ORDER ...
But we need more functionality. Can we make the row_number to repeat counting from 1 after a specific field changes in value. I know how to achieve what I want using a cursor within a stored procedure. Is there a way we achieve the same just using sql statements and without stored procedure/loop
For example, we have built the following resultset using an sql query and the result is already sorted by the first field, CompanyName:
CompanyName, department, ManagerName
A-company, Account, Steven
A-company, HR, Mark
A-company, IT, Susan
B-Company,IT,Ross
B-Company,Customer Service, Jack
C-Company,Finance, Matthew
C-Company,Finance, Helen
C-Company,Finance, Ron
I want to have a rew result with one additional column which is incremented automatically but the value must re-start from 1 once the Companyname changes.
1, A-company, Account, Steven
2, A-company, HR, Mark
3, A-company, IT, Susan
1, B-Company,IT,Ross
2, B-Company,Customer Service, Jack
1, C-Company,Finance, Matthew
2, C-Company,Finance, Helen
3, C-Company,Finance, Ron
Please enlighten me.