I am having SQLdata in my Employee table like below. EmpId is primary key and TName is having empty string with no value
| EmpId | TName | GrId
| -------- | -------- | ---------
| 1 | | AA
-----------------------------------
| 2 | | AA
-----------------------------------
| 3 | | BB
-----------------------------------
| 4 | | BB
-----------------------------------
| 5 | | BB
-----------------------------------
I want to auto increment the TName value based on the grouping of this column “GrId”
For EmpId = 1 and 2 we are having same GrId so we can start incremented with +1 and for EmpId=3,4,5
we are having another GrId so again start incrementing from beginning (zero) with +1
I cannot use Identity constraint on my TName column
I have tried like this but thorwing errr
Update T
Set T.TName = '('+ (select CAST(Count(*) + 1 AS Nvarchar(max)) from Employee) + ')'
from Employee T
Where 1=1
group by T.GrId
My expected output:
| EmpId | TName | GrId
| -------- | -------- | ---------
| 1 | (1) | AA
-----------------------------------
| 2 | (2) | AA
-----------------------------------
| 3 | (1) | BB
-----------------------------------
| 4 | (2) | BB
-----------------------------------
| 5 | (3) | BB
-----------------------------------
1