日期:2014-05-18  浏览次数:20549 次

看看这个统计更新的SQL怎么写呢?急....在线等...
HTML code

MS SQL2000,表tab1中,数据如下:

id   stu    kind   account
1    小明   a
2    阿东   a
3    李四   b
4    杨尚   c
5    阿强   a
6    阿基   b
7    要求   a
8    文东   a

我想实现,按kind统计出每个分类的数量,并更新到account字段,如下:

id   stu    kind   account
1    小明    a       5
2    阿东    a       5
3    李四    b       2
4    杨尚    c       1
5    阿强    a       5
6    阿基    b       2
7    要求    a       5
8    文东    a       5

请问这个更新的SQL语句怎么写呢?在线等。。。。




------解决方案--------------------
SQL code
update a
set a.account =b.counts
from tab1 a, (select kind,count(1)as counts from tab1 group by kind)b
where a.kind=b.kind

------解决方案--------------------
SQL code

update tab1 set account=
(select count(1) from tab1 where kind=t.kind) from tab1 t