这些数分别属于不同的区域01 05 09 13 是属于1区
02 06 10 14 是属于2区
03 07 11 15 是属于3区
04 08 12 16 是属于4区
现在有一列记录
LH   
16
15
02
04
05
05
02
10
06
04
14
13
05
06
16
13
13
06
03
02
04
13
03
11
06
02
04
14执行语句后LH[em]所属区域
16[em]4-------'16'这个数字属于第‘4’区域,记作‘4’15[em]3
02[em]2
04[em]4
05[em]1
05[em]1
02[em]2
10[em]2-----'10'这个数字属于第‘2’区域,记作‘2’06[em]2,以此类推04[em]4
14[em]2
13[em]1
05[em]1
06[em]2
16[em]4
13[em]1
13[em]1
06[em]2
03[em]3
02[em]2
04[em]4
13[em]1
03[em]3
11[em]3
06[em]2
02[em]2
04[em]4
14[em]2
请问用SQL语句如何完成?谢谢

解决方案 »

  1.   

    select case when lh in ('01', '05', '09', '13') then lh+ '[em]1'
      when lh in ('02', '06', '10', '14') then lh+ '[em]2'
      when lh in ('03', '07', '11', '15') then lh+ '[em]3'
      when lh in ('04', '08', '12', '16') then lh+ '[em]4' end as result
    from tab
      

  2.   

    执行语句后LH[em]所属区域
    16[em]4-------'16'这个数字属于第‘4’区域,记作‘4’
    15[em]3
    02[em]2
    04[em]4
    05[em]1
    05[em]1
    02[em]2
    10[em]2-----'10'这个数字属于第‘2’区域,记作‘2’,以此类推
    06[em]2
    04[em]4
    14[em]2
    13[em]1
    05[em]1
    06[em]2
    16[em]4
    13[em]1
    13[em]1
    06[em]2
    03[em]3
    02[em]2
    04[em]4
    13[em]1
    03[em]3
    11[em]3
    06[em]2
    02[em]2
    04[em]4
    14[em]2
    请问用SQL语句如何完成?谢谢
      

  3.   

    稍改下即可
    select lh,case when lh in ('01', '05', '09', '13') then '1'
      when lh in ('02', '06', '10', '14') then '2'
      when lh in ('03', '07', '11', '15') then '3'
      when lh in ('04', '08', '12', '16') then '4' end as AddField
    from tab
      

  4.   

    select case 
      when LH in ('01', '05', '09', '13') then LH as 1
      when LH in ('02', '06', '10', '14') then LH as 2
      when LH in ('03', '07', '11', '15') then LH as 3
      when LH in ('04', '08', '12', '16') then LH as 4 
      end as NQQFROM [master].[dbo].[1074历史号码数据]
    我的问题就是:这个语句有错,高手帮忙修改
      

  5.   

    if object_id ('tb') is not null
    drop table tb
    go
    create table tb (LH varchar(2))
    insert into tb
    select '16' union all
    select '15' union all
    select '02' union all
    select '04' union all
    select '05' union all
    select '05' union all
    select '02' union all
    select '10' union all
    select '06' union all
    select '04' union all
    select '14' union all
    select '13' union all
    select '05' union all
    select '06' union all
    select '16' union all
    select '13' union all
    select '13' union all
    select '06' union all
    select '03' union all
    select '02' union all
    select '04' union all
    select '13' union all
    select '03' union all
    select '11' union all
    select '06' union all
    select '02' union all
    select '04' union all
    select '14'declare @tb table (lh varchar(20),区域 int)
    insert into @tb
    select '01 05 09 13', 1 union all
    select '02 06 10 14', 2 union all
    select '03 07 11 15', 3 union all
    select '04 08 12 16', 4
    select a.LH,b.区域 as 所属区域
    from tb a join (select lh=SUBSTRING(a.lh,number,CHARINDEX(',',a.lh+',',number)-b.number),a.区域
                    from( select REPLACE(lh,' ',',') lh, 区域
                          from @tb
                   ) a join master..spt_values b ON b.type='p'
                     and CHARINDEX(',',','+a.lh,number)=number) b on a.LH=b.lhLH 所属区域
    05 1
    05 1
    05 1
    13 1
    13 1
    13 1
    13 1
    02 2
    02 2
    02 2
    02 2
    06 2
    06 2
    06 2
    06 2
    10 2
    14 2
    14 2
    03 3
    03 3
    11 3
    15 3
    04 4
    04 4
    04 4
    04 4
    16 4
    16 4