结构如
id          level
100000001   0
....
100000110   0
100000111   1
....
100001234   2最后面三个相同或连续,level 更新为 1
超过三个,level 更新为 2
普通的就是 level 更新为 0请大侠帮忙,谢谢

解决方案 »

  1.   


    with tb(a) as(
    select 100000001 union
    select 100000110 union
    select 100000111 union
    select 100001234
    )
    select a,case when CONVERT(int,right(a,3))%111=0 then 2 
                  when (CONVERT(int,right(a,3))+12)%111=0 then 1  
                  when (CONVERT(int,right(a,3))-12)%111=0 then 1  
                  else 0 end from tb
      

  2.   

    谢谢sc273607742
    但是,做了下测试,有些不对,比如后3位111这个应该是1,后4位为1234这个应该是2
    为2的条件是超过3个相同或者连续
    为1的条件就是3个相同或者连续
    10000109 0
    10000110 0
    10000111 2
    10001234 1
    10002222 2
    10001111 2
    10012345 1
    10033333 2
    with tb(a) as(
    select 10000109 union 
    select 10000110 union 
    select 10000111 union 
    select 10001234 union 
    select 10002222 union 
    select 10001111 union 
    select 10012345 union 
    select 10033333 )
    select *,
    case when  RIGHT(a,4)%1111=0 then 2 
         when  (RIGHT(a,4)-1234)%1111=0 then 2
         when  RIGHT(a,3)%111=0 then 1
         when  (RIGHT(a,3)-123)%111=0 then 1
         else 0
    end from tb 
      

  3.   

    谢谢sc273607742
    其他基本都对了,还有一个10000123即3位的连续号,这个通过你的SQL得到的结果是2,应该是1
      

  4.   

    谢谢sc273607742
    其他基本都对了,还有一个10000123即3位的连续号,这个通过你的SQL得到的结果是2,应该是1
    select *,
    case when  RIGHT(a,4)%1111=0 and RIGHT(a,4)>1000 then 2 
         when  (RIGHT(a,4)-1234)%1111=0  and RIGHT(a,4)>1000 then 2
         when  RIGHT(a,3)%111=0 then 1
         when  (RIGHT(a,3)-123)%111=0 then 1
         else 0
    end from tb 
      

  5.   

    谢谢sc273607742
    其他基本都对了,还有一个10000123即3位的连续号,这个通过你的SQL得到的结果是2,应该是1那10000000,11000000这类整数呢?都是2吗?
      

  6.   


    select *,
    case when  RIGHT(a,5)%10000=0 then 2
         when  RIGHT(a,4)%1111=0 and RIGHT(a,4)>1000 then 2 
         when  (RIGHT(a,4)-1234)%1111=0  and RIGHT(a,4)>1000 then 2
         when  RIGHT(a,3)%111=0 then 1
         when  (RIGHT(a,3)-123)%111=0 then 1
         else 0
    end from tb 
    这个应该差不多了
      

  7.   


    select *,
    case when  RIGHT(a,5)%10000=0 then 2
         when  RIGHT(a,4)%1111=0 and RIGHT(a,4)>1000 then 2 
         when  (RIGHT(a,4)-1234)%1111=0  and RIGHT(a,4)>1000 then 2
         when  RIGHT(a,3)%111=0 then 1
         when  (RIGHT(a,3)-123)%111=0 then 1
         when right(a,3)='012' then 1
         else 0
    end 
    from tb 
      

  8.   

    感谢dcy0927和sc273607742
    怎么会怪呢,dcy0927你说的对,我也并不是没有想,最后一个我也知道再增加判断条件,也测试了,加好了。
    不知道有没有其他更好的方法。