My table:
|ID |data | cr | | 1 | AAA | | | 2 | AAA | | | 3 | AAA | | | 4 | BBB | | | 5 | CCC | | | 6 | BBB | |
I need the result:
|ID |data | cr | | 1 | AAA | 3 | | 2 | AAA | 3 | | 3 | AAA | 3 | | 4 | BBB | 2 | | 5 | CCC | 1 | | 6 | BBB | 2 |
Found this Update the column value to the COUNT row for certain values โโin the same table and tried it:
UPDATE MyTbl a, (SELECT data,COUNT(*) cnt FROM MyTbl GROUP BY data) b SET a.cr = b.cnt WHERE a.data= b.data
SQL Server gives an error:
Msg 102, Level 15, State 1, Line 1 Incorrect syntax near 'a'. Msg 102, Level 15, State 1, Line 4 Incorrect syntax near 'b'.
Any idea how to do this in SQL Server (2014 Express).
Thanks in advance.
source share