根据年龄和身高分类显示姓名和总成绩
select name,sum(sorce) 
from student
where age =20 and hight>=160
age =21 and hight>=165
age =22 and hight>=170
age =23 and hight>=175我用union all 分开实现的
select name,sum(sorce) 
from student
where age =20 and hight>=160
union all
select name,sum(sorce) 
from student
where age =21 and hight>=165

解决方案 »

  1.   

    用with语句,这样效率能高不少
      

  2.   

    with my_test_block as
    (select deptno, ename from emp)
    select * from my_test_block
    union all
    select * from my_test_block
    这样,查询一次,然后在查询结果中进行再查询,效率更高
      

  3.   


    select name,sum(sorce)
    from student
    where 
    case age when 20 then hight>=160
    when 21 then hight>=165
    when 22 then hight>=170
    when 23 then hight>=175
    group by name

      

  4.   

    没看明白问题是想说什么
    分组可以用 group by ... having .....
    或多个查询union all
      

  5.   

    select age,sum(sorce)
    (select name, case when (age=20 and hight>=160) or
                         (age=21 and hight>=165) or
                         (age=22 and hight>=170) or
                         (age=23 and hight>=175) then sorce end sorce,from student)  
    where score is not null
    group by age
    这样应该可以满足楼主的要求把
    不过根据年龄分组,姓名就得从结果里去掉了
      

  6.   

    少了个字段
    select age,sum(sorce)
    (select name, age,
            case when (age=20 and hight>=160) or
                         (age=21 and hight>=165) or
                         (age=22 and hight>=170) or
                         (age=23 and hight>=175) then sorce end sorce,from student)  
    where score is not null
    group by age
      

  7.   

    用or
    select name,sum(sorce) 
    from student 
    where (age =20 and hight>=160 )
    or
    (age =21 and hight>=165 )
    or
    (age =22 and hight>=170 )
    or
    (age =23 and hight>=175 )
    group by name