My Table is:
|ID |data | cr |
| 1 | AAA | |
| 2 | AAA | |
| 3 | AAA | |
| 4 | BBB | |
| 5 | CCC | |
| 6 | BBB | |
I need 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 a column value to the COUNT of rows for specific values in 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 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.