mysql单表统计不同类型计数实例

mysql单表统计不同类型计数实例

在这里插入图片描述

SELECT
count(*) as total,
ifnull(sum(case when sex = ‘男’ then 1 else 0 end),0) as man,
ifnull(sum(case when sex = ‘女’ then 1 else 0 end),0) as woman,
ifnull(sum(case when political_status = ‘党员’ then 1 else 0 end),0) as communist,
ifnull(sum(case when political_status = ‘团员’ then 1 else 0 end),0) as communistYouth,
ifnull(sum(case when political_status = ‘群众’ then 1 else 0 end),0) as masses,
ifnull(sum(case when schooling = ‘小学’ then 1 else 0 end),0) as primarySchool,
ifnull(sum(case when schooling = ‘初中’ then 1 else 0 end),0) as middleSchool,
ifnull(sum(case when schooling = ‘高中’ then 1 else 0 end),0) as highSchool,
ifnull(sum(case when schooling = ‘本科’ then 1 else 0 end),0) as undergraduate,
ifnull(sum(case when schooling = ‘研究生’ then 1 else 0 end),0) as postgraduate,
ifnull(sum(case when identity = ‘0’ then 1 else 0 end),0) as selfWorkers,
ifnull(sum(case when identity = ‘1’ then 1 else 0 end),0) as outWorker,
ifnull(sum(case when identity = ‘2’ then 1 else 0 end),0) as labor,
ifnull(sum(case when identity = ‘3’ then 1 else 0 end),0) as hourlyWorker,
ifnull(sum(case when level is null then 1 else 0 end),0) as notRated,
ifnull(sum(case when level = ‘初级’ then 1 else 0 end),0) as elementary,
ifnull(sum(case when level = ‘中级’ then 1 else 0 end),0) as intermediate,
ifnull(sum(case when level = ‘高级’ then 1 else 0 end),0) as senior,
ifnull(sum(case when level = ‘技师’ then 1 else 0 end),0) as technician,
ifnull(sum(case WHEN (TIMESTAMPDIFF(YEAR,cnpc_employee.birth,NOW()) BETWEEN 21 AND 30) THEN 1 else 0 end),0) as youngPeople,
ifnull(sum(case WHEN (TIMESTAMPDIFF(YEAR,cnpc_employee.birth,NOW()) BETWEEN 31 AND 40) THEN 1 else 0 end),0) as earlyMiddleAged,
ifnull(sum(case WHEN (TIMESTAMPDIFF(YEAR,cnpc_employee.birth,NOW()) BETWEEN 41 AND 50) THEN 1 else 0 end),0) as middleAged,
ifnull(sum(case WHEN (TIMESTAMPDIFF(YEAR,cnpc_employee.birth,NOW()) BETWEEN 51 AND 60) THEN 1 else 0 end),0) as oldPeople
FROM
cnpc_employee
WHERE
STATUS = 0
AND on_job = 0
AND hose != ‘12356’
AND (type = 1 OR type = 2)

– and city = #{city}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值