我想计算每周超过40小时的工作时间。
table = shift_log_data
database = shift_logs
Field Type Null Key Default Extra
id int(100) NO PRI NULL auto_increment
admin_hrs decimal(20,2) NO 0.00
shop_hrs decimal(20,2) NO 0.00
fab_hrs decimal(20,2) NO 0.00
paint_hrs decimal(20,2) NO 0.00
vacation_hrs decimal(50,2) NO 0.00
control_hrs decimal(20,2) NO 0.00
test_hrs decimal(20,2) NO 0.00
engine_hrs decimal(20,2) NO 0.00
hydraulic_hrs decimal(20,2) NO 0.00
699_paint varchar(100) NO 0.00
699_shop varchar(100) NO 0.00
maint_hrs varchar(100) NO 0.00
date date NO NULL
name varchar(50) NO NULL
job varchar(50) NO NULL
每周需要使用日期字段列admin_hrs
到maint_hrs
。{
如果小时数超过每周40小时,请创建一个名为加班的别名,其余时间超过四十小时。
我试过了。
SELECT SUM(overtime)
FROM
(
SELECT name,
IF(SUM(test_hrs+fab_hrs+control_hrs+shop_hrs+paint_hrs+engine_hrs+hydraulic_hrs)>40,
SUM(test_hrs+fab_hrs+control_hrs+shop_hrs+paint_hrs+engine_hrs+hydraulic_hrs))-40,
0) AS overtime
FROM shift_log_data
GROUP BY name
) TOTAL_OVERTIME
答案 0 :(得分:0)
试试这个:
SELECT s.Name, YEAR(s.Date), WEEKOFYEAR(s.date),
IF(SUM(test_hrs+fab_hrs+control_hrs+shop_hrs+paint_hrs+engine_hrs+hydraulic_hrs)>40,
SUM(test_hrs+fab_hrs+control_hrs+shop_hrs+paint_hrs+engine_hrs+hydraulic_hrs))-40,
0
) AS overtime
FROM shift_log_data s
GROUP BY s.Name, YEAR(s.Date), WEEKOFYEAR(s.date);