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

求教怎么写下面的修改语句
创建表
create table test
(  
  id int identity(1,1) not null,
  a int not null,
  b char(1) not null,
)

插入数据
insert into test values (0,'A') 
insert into test values (0,'B')
insert into test values (0,'B')
insert into test values (0,'A')
insert into test values (0,'B')

现在表里面的数据就是
id a b
1 0 A
2 0 B
3 0 B
4 0 A
5 0 B

怎么通过SQL修改语句把表里面的a列数据变成
id a b
1 1 A
4 2 A
2 1 B
3 2 B
5 3 B

当然表里面不止这几条数据,怎么样做循环判断给b列相同的数据标上a列中的数字?

------解决方案--------------------
SQL code
update t
set a=(select count(*) from test where b=t.b and id<=t.id)
from test t
go