用一句select完成各年龄段人数的统计以及多条select语句查询结果的统计

本文介绍了一种使用SQL进行年龄分段统计的方法,通过CASE WHEN语句将不同年龄段的人数进行分类统计,并展示了如何在同一查询中实现多个条件的计数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


--分类统计各年龄段人数

select count(a) ,count(b),count(c),count(d),count(e),count(f),count(g) from 
( 
select 
(case when age <50 then t.oid end) a,  
(case when age between 50 and 60 then t.oid end) b, 
(case when age between 60 and 70 then t.oid end) c, 
(case when age between 70 and 80 then t.oid end) d,
(case when age between 80 and 90 then t.oid end) e,
(case when age between 90 and 100 then t.oid end) f,
(case when age >100 then t.oid end) g
from oldpeopleinfor t 
)


--多条select语句的统计
select count(a),count(b) from 
( 
select 
(case when x.community in(select actualname from userinfor where type='社区') then x.oid end) a,
(case when x.street in(select itemnote from dictitem) then x.oid end) b

from oldpeopleinfor x

)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值