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
请问有比这个更简单更快的么?
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
请问有比这个更简单更快的么?
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岁的人数
所以 我就改成了如题的语句了。
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
数据库中6800条数据
你的耗时为:0.062秒。
我的耗时也为:0.062秒。
50万条数据 你的耗时为:2.875
我耗时为:2.031
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 年龄段,
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