如何计算多个条件的值以及如何设计数据库?

How to calculate value with multiple condition and how to design database?

我需要计算多个条件的数据

MinOTHrs    MaxOTHs     DayType          Rate
------------------------------          -----------
3             -         Working_day        18
4             11        Weekend            18
11            -         Weekend            36

例如 如果员工在 working_day > 3 小时加班。他们将获得 18 分(working_day 最高 18 分) 如果员工在周末加班 > 4 小时。但 < 11 小时。他们将获得 18 分 如果员工在周末加班 > 11 小时。他们将获得 36 的价格(周末最高 36) 你能建议我编码和设计数据库吗? 非常感谢

我可能会将大部分工作作为工作表和 OT 表之间的多个连接来完成。这样你就可以非常具体地了解你想要的场景。

下面是适合您的入门读物。它并不完美,但它应该让你朝着我认为你想去的地方走很长的路! (包含的 ID 仅用于调试目的 - 当然不需要)。

//setup

CREATE TABLE overTime (
    MinOTHrs integer,
    MaxOTHrs integer,
    DayType varchar(max),
    payRate integer
);

CREATE TABLE workLog (
    logID integer,
    empID integer,
    DayType varchar(max),
    hoursWorked integer
);

insert into overTime values (3, null, 'Working_day', 18);
insert into overTime values (4, 11, 'Weekend', 18);
insert into overTime values (11, null, 'Weekend', 36);

insert into workLog values (567, 1234, 'Working_day', 2);
insert into workLog values (568, 1234, 'Working_day', 5);
insert into workLog values (569, 1234, 'Weekend', 2);
insert into workLog values (570, 1234, 'Weekend', 9);
insert into workLog values (571, 1234, 'Weekend', 14);

//query
select 
    wl.logID
  , wl.empID
  , wl.DayType
  , wl.hoursWorked
  , coalesce(weekdayOverTime.MinOTHrs, weekendLowOverTime.MinOTHrs, weekendHighOverTime.MinOTHrs) as MINOTHrs
  , coalesce(weekdayOverTime.MaxOTHrs, weekendLowOverTime.MaxOTHrs, weekendHighOverTime.MaxOTHrs) as MAXOTHrs
  , coalesce(weekdayOverTime.payRate, weekendLowOverTime.payRate, weekendHighOverTime.payRate) as maxOTPayBracket

from workLog wl
left join overTime weekdayOverTime on wl.DayType = weekdayOverTime.dayType and wl.hoursWorked > weekdayOverTime.MinOTHrs and wl.dayType = 'Working_day'
left join overTime weekendLowOverTime on wl.DayType = weekendLowOverTime.dayType and wl.hoursWorked > weekendLowOverTime.MinOTHrs and wl.hoursWorked < weekendLowOverTime.MaxOTHrs and wl.dayType= 'Weekend'
left join overTime weekendHighOverTime on wl.DayType = weekendHighOverTime.dayType and wl.hoursWorked > weekendHighOverTime.MinOTHrs and weekendHighOverTime.MaxOTHrs is null and wl.dayType= 'Weekend'