本帖最后由 donduck 于 2010-07-31 06:07:04 编辑

解决方案 »

  1.   

    --> 测试数据:#
    if object_id('tempdb.dbo.#TB1') is not null drop table #TB1
    SELECT 1234 ID,5 NUM INTO #TB1
    UNION ALL SELECT 1235,8   
    UNION ALL SELECT 1236,15   
    UNION ALL SELECT 1237,21  if object_id('tempdb.dbo.#TB2') is not null drop table #TB2 
    SELECT 10 section,1 [level] INTO #TB2
    UNION ALL SELECT 20,2   
    UNION ALL SELECT 30,3
    UNION ALL SELECT 40,4--如果level 是的划分条件是固定的且有限的可以直接写。
    select id,num,
    case when num < 10 then 1 
         when num >=10 and num<20 then 2
         when num >=20 and num<30 then 3
         when num >=30 and num<40 then 4
    end [level]  from #TB1--如果level 是按表二规律分布的可以这样写
    select id,num,num/10+1 [level]  from #TB1
    --如果要根据表二动态决定的话还没想好。
      

  2.   

    --> 测试数据:#
    if object_id('tempdb.dbo.#TB1') is not null drop table #TB1
    SELECT 1234 ID,5 NUM INTO #TB1
    UNION ALL SELECT 1235,8   
    UNION ALL SELECT 1236,15   
    UNION ALL SELECT 1237,21  if object_id('tempdb.dbo.#TB2') is not null drop table #TB2 
    SELECT 10 section,1 [level] INTO #TB2
    UNION ALL SELECT 20,2   
    UNION ALL SELECT 30,3
    UNION ALL SELECT 40,4--如果level 是的划分条件是固定的且有限的可以直接写。
    select id,num,
    case when num< 10 then 1 
         when num >=10 and num<20 then 2
         when num >=20 and num<30 then 3
         when num >=30 and num<40 then 4
    end [level]  from #TB1--如果level 是按表二规律分布的可以这样写
    select id,num,num/10+1 [level]  from #TB1
    --如果要根据表二动态决定,且level连续。
    select id,num,[level] from #TB1 t1 join 
    (select [level],(select section from #tb2 where [level] = t.[level]-1) lower_section
    ,section upper_section from #TB2 t) t2 
    on t1.num >= isnull(t2.lower_section,-999999999) and t1.num < t2.upper_section--如果要根据表二动态决定,且level不一定连续。
    with TB2 AS(select row_number() over (order by [level]) rn ,* FROM #TB2)
    select id,num,[level] from #TB1 t1 join 
    (select [level],(select section from TB2 where rn = t.rn-1) lower_section
    ,section upper_section from TB2 t) t2 
    on t1.num >= isnull(t2.lower_section,-999999999) and t1.num < t2.upper_section
      

  3.   

    --> 测试数据: #ta
    if object_id('tempdb.dbo.#ta') is not null drop table #ta
    go
    create table #ta (ID int,NUM int)
    insert into #ta
    select 1234,5 union all
    select 1235,8 union all
    select 1236,15 union all
    select 1237,21
    --> 测试数据: #tb
    if object_id('tempdb.dbo.#tb') is not null drop table #tb
    go
    create table #tb (section int,level int)
    insert into #tb
    select 10,1 union all
    select 20,2 union all
    select 30,3 union all
    select 40,4
    select id,level from #ta a,
    (select s1=b.section,s2=a.section-1,a.level from #tb a left join #tb b on a.level=b.level+1
    )b
    where a.num between isnull(s1,0) and s2
    id          level
    ----------- -----------
    1234        1
    1235        1
    1236        2
    1237        3(4 行受影响)
      

  4.   


    if object_id('tb1') is not null drop table tb1
    go
    create table tb1 (ID int,NUM int)
    insert into tb1
    select 1234,5 union all
    select 1235,8 union all
    select 1236,15 union all
    select 1237,21if object_id('tb2') is not null drop table tb2
    go
    create table tb2 (section int,level int)
    insert into tb2
    select 10,1 union all
    select 20,2 union all
    select 30,3 union all
    select 40,4select t.id,t1.level from 
    (select id,
    case when num < 10 then 10 when num >= 10 and num < 20 then 20 when num >=20 and num < 30 then 30 when num >= 30 and num < 40 then 40 else 50 end 
    as section from tb1 ) t inner join tb2 t1 on t.section = t1.section/* 结果
    id          level       
    ----------- ----------- 
    1234        1
    1235        1
    1236        2
    1237        3(所影响的行数为 4 行)
    */