mysql代码

项目场景:

mysql学习


问题描述:

表中数据如下:

dept_idstatusratio
A00.1
B10.2
C00.3
A30.4
B20.5

输出数据如下:  

dept_idstatus_0status_1status_2status_3
Asum(ratio)sum(ratio)sum(ratio)sum(ratio)
Bsum(ratio)sum(ratio)sum(ratio)sum(ratio)
Csum(ratio)sum(ratio)sum(ratio)sum(ratio)

解决方案:

#方案一:
select
	dept_id as dept_id,
	count(*) as num,
    count(*) as jiaGong,
	0 as guZhang,
	0 as kongXian,
	0 as guanJi,
	sum(utilize_ratio) as utilize_ratio,
	sum(run_time) as run_time,
	sum(fault_time) as fault_time,
	sum(free_time) as free_time,
	sum(close_time) as close_time
from
	adm_gallop_machine_worktime_today
where
	machine_location <> '暂不考核' 
	and command_name='加工'
group by dept_id
union
select
	dept_id,
    count(*),
	0,
	count(*),
	0,
	0,
	sum(utilize_ratio),
	sum(run_time),
	sum(fault_time),
	sum(free_time),
	sum(close_time)
from
	adm_gallop_machine_worktime_today
where
	machine_location <> '暂不考核'
	and command_name='网络故障'
group by dept_id
union
select
	dept_id,
    count(*),
	0,
	0,
	count(*),
	0,
	sum(utilize_ratio),
	sum(run_time),
	sum(fault_time),
	sum(free_time),
	sum(close_time)
from
	adm_gallop_machine_worktime_today
where
	machine_location <> '暂不考核'
	and command_name='空闲'
group by dept_id
union
select
	dept_id,
    count(*),
	0,
	0,
	0,
	count(*),
	sum(utilize_ratio),
	sum(run_time),
	sum(fault_time),
	sum(free_time),
	sum(close_time)
from
	adm_gallop_machine_worktime_today
where
	machine_location <> '暂不考核' 
	and command_name='关机'
group by dept_id;

#方案二:
select
	dept_id as fir,
	count(*),
	sum( case when command_name = '加工' then 1 else 0 end ),
	sum( case when command_name = '网络故障' then 1 else 0 end ),
	sum( case when command_name = '空闲' then 1 else 0 end ),
	sum( case when command_name = '关机' then 1 else 0 end ),
    sum(utilize_ratio)/count(*)*100,
	sum( run_time ) as run_time,
	sum( fault_time ) as fault_time,
	sum( free_time ) as free_time,
	sum( close_time ) as close_time 
from
	adm_gallop_machine_worktime_today 
where
	machine_location <> '暂不考核' 
group by
	dept_id;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值