情部是这样的
我有这么一张
CPBH             LSH
6666122            0
6666122            0
6666122            0
6666133            0
6666133            0
5454465            0
5451165            0
4541165            0 
4541165            0
5555511            0
我要变用语句变成:
CPBH             LSH
6666122            1
6666122            2
6666122            3 
6666133            1 
6666133            2
5454465            1
5451165            1
4541165            2 
4541165            3
5555511            1
这句语句怎么写呢?大家能否帮忙一下

解决方案 »

  1.   

    情况是这样的
    我有这么一张
    CPBH             LSH
    6666122            0
    6666122            0
    6666122            0
    6666133            0
    6666133            0
    5454465            0
    5451165            0
    4541165            0 
    4541165            0
    5555511            0
    我要用语句变成:
    CPBH             LSH
    6666122            1
    6666122            2
    6666122            3 
    6666133            1 
    6666133            2
    5454465            1
    5451165            1
    4541165            2 
    4541165            3
    5555511            1
    这句语句怎么写呢?大家能否帮忙一下
      

  2.   

    是否如下?结果与楼主的有出入~~~create table test (CPBH char(7),LSH int default 0)
    insert test(CPBH) select '6666122'
    insert test(CPBH) select '6666122'
    insert test(CPBH) select '6666122'
    insert test(CPBH) select '6666133'
    insert test(CPBH) select '6666133'
    insert test(CPBH) select '5454465'
    insert test(CPBH) select '5451165'
    insert test(CPBH) select '4541165'
    insert test(CPBH) select '4541165'
    insert test(CPBH) select '5555511'
    --select * from testalter table test 
    add id int identity(1,1)
    goupdate test set 
    LSH=
    (
    select count(*)+1 from test where cpbh=a.cpbh and id<a.id
    ) from test a alter table test
    drop column id
    goselect * from testdrop table test
      

  3.   

    create table #tab1(cpbh int,lsh int)
    insert into #tab1 select 6666122,            0
    union all         select 6666122   ,         0
    union all         select 6666122    ,       0
    union all         select 6666133    ,        0
    union all         select 6666133   ,         0
    union all         select 5454465   ,         0
    union all         select 5451165   ,         0
    union all         select 4541165   ,         0 
    union all         select 4541165   ,         0
    union all         select 5555511   ,         0declare @table table(cpbh int,id int )
    declare @a int ,@b int
    select @a=(select min(cpbh) from #tab1)
    select @b=1
    while @a<=(select max(cpbh) from #tab1)
      begin
          while @b<=(select count(0) from #tab1 where cpbh=@a)
             begin
              insert into @table(cpbh,id)
               values(@a,@b)
              select @b=@b+1
             end
          select @b=1
       select @a=(select min(cpbh) from #tab1 where cpbh>@a)
      end
    delete from #tab1
    insert into #tab1
    select *from @table order by cpbh descselect * from #tab1