让我们说
工作人员从2016-12-31 23:30 PM
到2017-01-01 06:30 AM
工作。
在这里,我需要根据以下规则计算员工的总工资率:
Rule# | Date | StartTime | EndTime | WageRate(HR)
1 | 2016-12-31 | 07:00 | 23:59 | 25
2 | 2017-01-01 | 00:00 | 06:00 | 40
3 | 2017-01-01 | 06:00 | 11:30 | 30
最终结果应考虑以下程序:
On 2016-12-31
23:30 - 23:59 = 29 min of wage
这应该来自规则#1,因为工作时间在07:00 - 23:59之间。
类似地,
On 2017-01-01
00:00 - 06:00 = 6 HR wage
这应该来自规则#2
06:00 - 06:30= 30 min wage
这应该来自规则#3
我尝试将开始工作时间增加一小时,但在规则#1和规则#2的情况下失败。
答案 0 :(得分:0)
在这个解决方案中,我使用一个名为dates的帮助表,看起来像这样
TextView
所以给出
MariaDB [sandbox]> select dte from dates where dte between '2016-12-31' and '2017-01-01';
+------------+
| dte |
+------------+
| 2016-12-31 |
| 2017-01-01 |
+------------+
2 rows in set (0.02 sec)
此查询
drop table if exists WageRate;
create table WageRate (Rule int , Dte date, StartTime time, EndTime time, WageRate int);
insert into wagerate values
(1 , '2016-12-31' ,'07:00' , '23:59' , 25),
(2 , '2017-01-01' ,'00:00' , '06:00' , 40),
(3 , '2017-01-01' ,'06:00' , '11:30' , 30);
drop table if exists EmployeeHours;
create table EmployeeHours (id int, startdatetime datetime, enddatetime datetime);
truncate table employeehours;
insert into EmployeeHours values
(1,'2016-12-31 23:30:00' , '2017-01-01 06:30:00'),
(2,'2016-12-31 23:30:00' , '2017-01-01 06:00:00');
返回
select s.*,
timediff(s.newet,s.newst) TimeDifference
from
(
select eh.*,d.dte d_dte,wr.*,
case when cast(concat(wr.dte,' ',wr.starttime) as datetime) >= eh.startdatetime then
cast(concat(wr.dte,' ',wr.starttime) as datetime)
else eh.startdatetime
end as newst,
case when cast(concat(wr.dte,' ',wr.endtime) as datetime) <= eh.enddatetime then
cast(concat(wr.dte,' ',wr.endtime) as datetime)
else eh.enddatetime
end as newet
from EmployeeHours eh, dates d
join WageRate wr on wr.dte = d.dte
where d.dte between '2016-12-31' and '2017-01-01'
) s
where timediff(s.newet,s.newst) <> 0