当前位置: 代码迷 >> Sql Server >> 求教如何写下面的修改语句
  详细解决方案

求教如何写下面的修改语句

热度:44   发布时间:2016-04-27 14:37:42.0
求教怎么写下面的修改语句
创建表
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 tset a=(select count(*) from test where b=t.b and id<=t.id)from test tgo
  相关解决方案