现有用户表A
ID Name Age
------------
1 a 23
2 b 25
3 c 32
4 d 21
5 e 19
6 f 35
7 g 26
8 h 36
9 i 34范围表B
ID LAge HAge
------------
1 18 25
2 26 30
3 31 35
4 36 40
5 40 希望写一条SQL语句
执行后得到下面的结果:表B
Range PersonNum
----------------
18-25 4
26-30 1
31-35 3
36-40 1
40以上 0请问这样的语句该如何写?

解决方案 »

  1.   

    select
        rtrim(B.LAge)+case when B.HAge is null '以上' then  else '-'+rtrim(B.HAge) end as Range,
        count(A.Age) as PersonNum
    from
        B,A
    where
        A.Age between B.LAge and isnull(B.HAge,100)
    group by
        B.LAge,B.HAge
      

  2.   

    select range,count(*) from 
    (select t.range,a.* from a inner join 
    (select *,LAge+case when hAge<>'' then '-'+hgage else '以上' as range from B) t
    on a.age between lage and hage ) t1 
    group by range order by range
      

  3.   

    select
        rtrim(B.LAge)+case when B.HAge is null then '以上' else '-'+rtrim(B.HAge) end as Range,
        count(A.Age) as PersonNum
    from
        B,A
    where
        A.Age between B.LAge and isnull(B.HAge,100)
    group by
        B.LAge,B.HAge
      

  4.   

    declare @a table(ID int,Name char(1),Age int)
    insert into @a
         select 1,'a',23
    union select 2,'b',25
    union select 3,'c',32
    union select 4,'d',21
    union select 5,'e',19
    union select 6,'f',35
    union select 7,'g',26
    union select 8,'h',36
    union select 9,'i',34declare @b table(ID int,LAge int,HAge int)
    insert into @b
          select 1,18,25
    union select 2,26,30
    union select 3,31,35
    union select 4,36,40
    union select 5,40,nullselect
        rtrim(B.LAge)+case when B.HAge is null then '以上' else '-'+rtrim(B.HAge) end as Range,
        isnull(count(A.Age),0) as PersonNum
    from
        @B b
    left join
        @A a
    on
        A.Age between (case when B.HAge is null then B.LAge+1 else B.LAge end) and isnull(B.HAge,100)
    group by
        B.ID,B.LAge,B.HAge
    order by
        B.ID/*
    Range                     PersonNum   
    ------------------------- ----------- 
    18-25                     4
    26-30                     1
    31-35                     3
    36-40                     1
    40以上                     0
    */
      

  5.   

    libin_ftsafe(子陌红尘) 的答案写的比我的好,要外联才行,疏忽了。
      

  6.   

    declare @a table(ID int,Name char(1),Age int)
    insert into @a
         select 1,'a',23
    union select 2,'b',25
    union select 3,'c',32
    union select 4,'d',21
    union select 5,'e',19
    union select 6,'f',35
    union select 7,'g',26
    union select 8,'h',36
    union select 9,'i',34declare @b table(ID int,LAge int,HAge int)
    insert into @b
          select 1,18,25
    union select 2,26,30
    union select 3,31,35
    union select 4,36,40
    union select 5,40,null
    select cast(lage as varchar)+'-'+isnull(cast(hage as varchar),'以上')Range
    ,(select count(*) from @a where age between b.lage and b.hage)  PersonNum
    from @b b
      

  7.   

    create table xx(ID int,Name char(1),Age int)
    insert into xx
         select 1,'a',23
    union select 2,'b',25
    union select 3,'c',32
    union select 4,'d',21
    union select 5,'e',19
    union select 6,'f',35
    union select 7,'g',26
    union select 8,'h',36
    union select 9,'i',34create  table xxx(ID int,LAge int,HAge int)
    insert into xxx
          select 1,18,25
    union select 2,26,30
    union select 3,31,35
    union select 4,36,40
    union select 5,40,null
    select range,sum(ss) as personnum from 
    (
    select 'Range'=rtrim(lage)+'-'+isnull(rtrim(hage),100),
    case when age between x.lage and x.hage then 1 else 0  end as ss from xx ,xxx x)a  group by range order by range