I have a table as follows, I want to automatically increase the SIRANO value according to the rows with the same MF_ID value in this table.
| SIRANO | MF_ID |
|---|---|
| NULL | 30 |
| NULL | 30 |
| NULL | 30 |
| NULL | 31 |
| NULL | 31 |
| NULL | 32 |
must be:
| SIRANO | MF_ID |
|---|---|
| 1 | 30 |
| 2 | 30 |
| 3 | 30 |
| 1 | 31 |
| 2 | 31 |
| 1 | 32 |
DECLARE @counter int
SET @counter = 0
update A
set SIRANO = (case when A.MF_ID=B.MF_ID then @counter+1
else 0
end)
FROM MYTABLE_0002 a
INNER JOIN MYTABLE_0002 b
on a.MF_ID = b.MF_ID