我现在有一个表,如下
xm     sf
张三   浙江
李四   上海
王五   浙江
何八   浙江
吕九   江苏
黄大   江苏
徐十   上海我现在要增加一个字段SXH,他的要求就是按SF,从001开始递增,就是要变为以下的格式xm     sf    sxh
张三   浙江  001
李四   上海  001
王五   浙江  002
何八   浙江  003
吕九   江苏  001
黄大   江苏  002 
徐十   上海  002

解决方案 »

  1.   

    declare @t table(xm varchar(10),sf varchar(10))
    insert into @t select '张三','浙江'
    union all select '李四','上海'
    union all select '王五','浙江'
    union all select '何八','浙江'
    union all select '吕九','江苏'
    union all select '黄大','江苏'
    union all select '徐十','上海'select [id]=identity(int,1,1),* into # from @t
    select xm,sf,sxh=(select '00'+cast(count(*)+1 as varchar) from # where sf=a.sf and id<a.id) from # adrop table #
      

  2.   

    楼上的大虾能解释一下
    select xm,sf,sxh=(select '00'+cast(count(*)+1 as varchar) from # where sf=a.sf and id<a.id) from # a的含义吗,使用了自连接,但我又有点看不懂,谢谢