A 表
id,出生日期
   我要求出:>60岁的人数(包含了>65的),
             :>65岁的人数
               :<60岁的人数(包含了<65的)
                :<65岁的人数
select case when (sysdate-出生日期)/365 <60 then '<60岁'
            when (sysdate-出生日期)/365 >=60 then '>=60岁'
end As 年龄段 ,
count(ID)  As 人数
from A
group by case when (sysdate-出生日期)/365 <60 then '<60岁'
            when (sysdate-出生日期)/365 >=60 then '>=60岁'
end
union 
select case when (sysdate-出生日期)/365 <65 then '<65岁'
            when (sysdate-出生日期)/365 >=65 then '>=65岁'
end As 年龄段 ,
count(ID)  As 人数
from A
group by case when (sysdate-出生日期)/365 <65 then '<65岁'
            when (sysdate-出生日期)/365 >=65 then '>=65岁'
end
请问有比这个更简单更快的么? 

解决方案 »

  1.   

    我记得书上说union all比union效率高。。
      

  2.   

    要是我做,我就先算出>60岁的人数(包含了>65的)和>65岁的人数,然后用总数去减,得出<60岁的人数(包含了 <65的) 和<65岁的人数 
      

  3.   

    应该是union all 的效率比union 的效率高,因为union 每次要做 重复数据判断。 而union all 就不用
      

  4.   

    #2:请问你这个语句应该怎么写 。 本来我开始的时候有一个统计表的 
    select count(id) As 人数,
    case when (sysdate-出生日期)<60 then '<60'
         when (sysdate-出生日期)>60 and (sysdate-出生日期)<65 then '60-65'
         when (sysdate-出生日期)>65 then '>65' 
    end As 年龄段
    from A
    group by 
    case when (sysdate-出生日期)<60 then '<60'
         when (sysdate-出生日期)>60 and (sysdate-出生日期)<65 then '60-65'
         when (sysdate-出生日期)>65 then '>65' 
    end;
    但是我不知道怎么 计算出  :>60岁的人数(包含了>65的), 
                :>65岁的人数 
                  : <60岁的人数(包含了 <65的) 
                    : <65岁的人数 
    所以 我就改成了如题的语句了。
      

  5.   

    没必要把它们弄成行去吧。你这个基本上全表遍历4次,很没效率。
    select sum(case when (sysdate-出生日期)/365 <60 then 1 else 0 end) as 60num,
    sum(case when (sysdate-出生日期)/365 >60 then 1 else 0 end) as d60num,
    sum(case when (sysdate-出生日期)/365 <65 then 1 else 0 end) as 65num,
    sum(case when (sysdate-出生日期)/365 >65 then 1 else 0 end) as d65num
     from A
      

  6.   

      我在plsql 中执行了一下,
    数据库中6800条数据 
      你的耗时为:0.062秒。
      我的耗时也为:0.062秒。
     50万条数据 你的耗时为:2.875 
              我耗时为:2.031 
      

  7.   

    select case
             when (sysdate - 出生日期) / 365 < 60 then
              ' <60岁'
             when (sysdate - 出生日期) / 365 >= 60 then
              '>=60岁'
           end As 年龄段60,
           case
             when (sysdate - 出生日期) / 365 < 65 then
              ' <65岁'
             when (sysdate - 出生日期) / 365 >= 65 then
              '>=65岁'
           end As 年龄段61,
           count(ID) As 人数
      from A
     group by case
                when (sysdate - 出生日期) / 365 < 60 then
                 ' <60岁'
                when (sysdate - 出生日期) / 365 >= 60 then
                 '>=60岁'
              end,
              case
             when (sysdate - 出生日期) / 365 < 65 then
              ' <65岁'
             when (sysdate - 出生日期) / 365 >= 65 then
              '>=65岁'
           end As 年龄段,
     
      

  8.   

    select '>60岁',count(*) from 表名 where round(months_between(sysdate,出生日期)/12,0)>60
    union all select '>65岁',count(*) from 表名 where round(months_between(sysdate,出生日期)/12,0)>65
    union all select '<60岁',count(*) from 表名 where round(months_between(sysdate,出生日期)/12,0)<60
    union all select '<65岁',count(*) from 表名 where round(months_between(sysdate,出生日期)/12,0)<65