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


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

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

)


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果你只有身份证号码,而没有具体的出生日期或年龄信息,那么无法直接通过SQL语句统计不同年龄段人数。因为身份证号码中并不包含出生日期的明确信息。 要进行年龄段统计,需要先根据身份证号码提取出出生日期,然后根据出生日期计算年龄,并将其归入相应的年龄段。这可能需要使用编程语言或其他工具来处理数据,并将处理后的数据导入到数据库中。 以下是一个示例的过程,假设你有一个身份证号码字段为 `id_card` 的表 `residents`: 1. 通过身份证号码提取出生日期: ```sql SELECT id_card, SUBSTRING(id_card, 7, 8) as birthdate FROM residents; ``` 上述SQL语句使用 `SUBSTRING` 函数从身份证号码中提取出生日期,假设出生日期在身份证号码的第7到第14位。 2. 根据出生日期计算年龄: ```sql SELECT id_card, YEAR(CURRENT_DATE) - YEAR(birthdate) as age FROM ( SELECT id_card, STR_TO_DATE(birthdate, '%Y%m%d') as birthdate FROM residents ) as temp; ``` 上述SQL语句使用 `STR_TO_DATE` 函数将出生日期转换为日期格式,然后通过 `YEAR` 函数计算年龄。 3. 将年龄归入不同年龄段,并统计人数: ```sql SELECT age_group, COUNT(*) as population_count FROM ( SELECT id_card, YEAR(CURRENT_DATE) - YEAR(birthdate) as age FROM ( SELECT id_card, STR_TO_DATE(birthdate, '%Y%m%d') as birthdate FROM residents ) as temp ) as temp2 GROUP BY age_group; ``` 上述SQL语句使用子查询将计算年龄的结果作为临时表,然后根据年龄段进行分组,并统计每个年龄段人数。 请注意,以上只是一个示例,并且假设出生日期在身份证号码中的特定位置。具体的处理方法可能需要根据身份证号码的格式和数据结构进行调整。此外,如果你的数据量非常大,建议使用合适的索引来提高查询效率。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值