按组别根据号码大小更新顺序号。
号码中有2种字符,1种是正常的数字,另一种是加号数字,如+1,+2.加号数字排在正常数字之后。号码大小如果是正常的数字按数字大小计算顺序号,如果是叫号数字,叫号数字在正常数字之后,叫号数字之间比较大小根据"+"符号后的数字大小。如
把无序数据:
       组别   号码(字符)            顺序号
0000 2 0
0000 1 0
0000 8 0
0000 +3 0
0000 +1 0
0000 +2 0
0000 12 0
1111     +2 0
1111 12 0
1111     13 0
1111 1 0
2222 3 0
2222 1 0
2222 +1 0
变成:       组别   号码(字符)               顺序号
0000 1 1
0000 2 2
0000 8 3
0000 12 4
0000 +1 5
0000 +2 6
0000 +3 7
1111     1 1
1111 12 2
1111     13 3
1111 +2 4
2222 1 1
2222 3 2
2222 +1 3请问如何实现如上更新顺序号的存储过程??

解决方案 »

  1.   

    select * from 表 order by 组别,case when charindex('+',号码)>0 then 1 else 0 end ,号码
      

  2.   

    jinjazz(近身剪(充电中...)) 的回答好像不对啊,请教高手!!!
      

  3.   

    SELECT * FROM 表 
    ORDER BY 组别, CHARINDEX('+', 号码), 号码
      

  4.   

    select * from 表 order by 组别,case when charindex('+',号码)>0 then 1 else 0 end ,cast(号码 as int)
      

  5.   

    我的比较麻烦不过可以实现
    declare @maxss int 
    select @maxss=max(cast(号码 as int)) from table1update table1 set  顺序号=(select count(*) from table1 b where a.组别=b.组别 and
    cast(ltrim(rtrim(replace(a.号码,'+',str(@maxss)))) as int)>
    cast(ltrim(rtrim(replace(b.号码,'+',str(@maxss)))) as int)) from table1 a
      

  6.   

    --用临时表,转换下吧!
    --测试环境
    declare @t table(组别 varchar(10),号码 varchar(10),顺序号 int)
    insert into @t select '0000','2',0
    union all select '0000','1',0
    union all select '0000','8',0
    union all select '0000','+3',0
    union all select '0000','+1',0
    union all select '0000','+2',0
    union all select '0000','12',0
    union all select '1111','+2',0
    union all select '1111','12',0
    union all select '1111','13',0
    union all select '1111','1',0
    union all select '2222','3',0
    union all select '2222','1',0
    union all select '2222','+1',0select * INTO # from @t order by 组别,case when charindex('+',号码)>0 then 1 else 0 end ,
    cast(号码 as int)
    declare @组别 int,@顺序号 int
    update 
       #
    set 
        @顺序号 = case when @组别=组别  then @顺序号+1 else 1 end,
        顺序号  = @顺序号,
        @组别 = 组别
    from #delete @t 
    insert @t select * from #drop table #--查看数据
    select * from @t--结果
    组别         号码         顺序号         
    ---------- ---------- ----------- 
    0000       1          1
    0000       2          2
    0000       8          3
    0000       12         4
    0000       +1         5
    0000       +2         6
    0000       +3         7
    1111       1          1
    1111       12         2
    1111       13         3
    1111       +2         4
    2222       1          1
    2222       3          2
    2222       +1         3(所影响的行数为 14 行)