1

我有这个问题,我无法解决,因为我认为这在 sql 中是不可能的

我有这张桌子

Shedule
    id_emp    Name       `time initial` time end   
    1         juan`       09:00`          12:00
    2        Francisco    10:00           11:30
    3        Sebastian    11:00           15:00
    6        Roberto      15:00           18:00


Suspension
id_emp          suspension_initial     suspension_end
1                    2013-06-01           2013-06-01
2                    2013-06-01           2013-06-03
3                    2013-06-03           2013-06-04 
6                    2013-06-01           2013-06-01
2                    2013-07-01           2013-07-01
3                    2013-07-05           2013-07-05
1                    2013-07-06           2013-07-06

我想了解工作时间((time_end- time_initial) - 暂停)(如果我有 1 天的暂停是一个小时单位示例:Juan 每天工作 3 小时,他在 6 月有 1 天的暂停和 1 天的暂停七月。所以我假设他在一个月内工作 3*20(小时*工作日)- 6 月 3 小时和 7 月 3 小时 我怎样才能得到这个结果

id_emp name `       June-2013               July-2013
1       Juan           57 (hours Worked)        57 (hours Worked)
2       Francisco      24 (hours worked)        27 (hours worked)
3       Sebastián
6       Roberto
4

1 回答 1

0

这是 SQLFiddel 演示

以下是您可以尝试的查询

select EmpHrs.ID_EMP,
       EmpHrs.Name,
       (
        (EmpHrs.NOOFHRS*20)-
        (EmpHrs.NOOFHRS*
          JuneSuspension.MONTHSUSPENSION)
       )  as "June-2013",
       (
        (EmpHrs.NOOFHRS*20)-
        (EmpHrs.NOOFHRS*
          JulySuspension.MONTHSUSPENSION)
       )  as "July-2013"

from 
(
select ID_EMP,NAME,
       Extract(Hours from time_end-time_initial)+
       Extract(Minutes from time_end-time_initial)/60 as NoOfHrs
  from schedule
) EmpHrs
Left join
(select ID_EMP,to_char(to_timestamp (Extract(Month from suspension_initial)::text, 'MM'),'Mon') as MonthIni,(suspension_end::date - suspension_initial::date)+1 MonthSuspension
from suspension
where Extract(Month from suspension_initial) = 6) JuneSuspension
On JuneSuspension.ID_EMP = EmpHrs.ID_EMP
Left join
(select ID_EMP,to_char(to_timestamp (Extract(Month from suspension_initial)::text, 'MM'),'Mon') as MonthIni,(suspension_end::date - suspension_initial::date)+1 MonthSuspension
from suspension
where Extract(Month from suspension_initial) = 7) JulySuspension
On JulySuspension.ID_EMP = EmpHrs.ID_EMP
于 2013-08-06T05:19:59.803 回答