在目标表中的两个日期之间的日期上连接表

时间:2017-08-09 14:14:08

标签: sql-server tsql

我有两张桌子

tbl_TimeEntries 
   EmployeeID int,
   StartDateTime datetime,
   EndDateTime datetime

tbl_Crew_Employees
   CrewID,
   EmployeeID,
   StartDate,
   EndDate

我还有一个查询可以产生每位员工每天工作的小时数,但我也希望包括当天员工所在的工作人员。

SELECT  tbl_TimeEntries.EmployeeID, 
        SUM(DATEDIFF(SECOND, StartDateTime, EndDateTime) / 60.0
                     / 60.0) as Hours,
        CAST(StartDateTime AS date) as WorkDate
FROM    tbl_TimeEntries 
GROUP BY tbl_TimeEntries.EmployeeID, CAST(StartDateTime AS date)
ORDER BY CAST(StartDateTime AS date)

我不确定如何在此查询中包含CrewID,因为tbl_Crew_Employees使用StartDate和EndDate(意味着员工是从StartDate到EndDate的员工)。我要么需要扩展StartDate / EndDate范围,要么使用某种我不知道的SQL魔法。

以下是来自tbl_Crew_Employees,tbl_TimeEntries和添加了所需列数据的当前查询的数据示例。 EmployeeID 88代表样本中的两个不同的工作人员。

CrewID  EmployeeID  StartDate   EndDate

13      11          2013-03-30  2013-05-12
12      88          2013-01-02  2013-04-18
12      66          2013-01-02  2013-06-30
13      88          2013-04-19  2013-04-21
11      111         2013-01-02  2013-04-28

EmployeeID  StartDateTime       EndDateTime
11          2013-04-18 08:00    2013-04-18 12:00
11          2013-04-18 12:30    2013-04-18 18:30
111         2013-04-18 10:00    2013-04-18 12:00
111         2013-04-18 12:30    2013-04-18 18:30
88          2013-04-18 11:00    2013-04-18 12:00
88          2013-04-18 12:30    2013-04-18 19:30
66          2013-04-18 10:00    2013-04-18 12:00
66          2013-04-18 12:30    2013-04-18 18:30
11          2013-04-20 08:00    2013-04-20 12:00
11          2013-04-20 12:30    2013-04-20 18:00
111         2013-04-20 10:00    2013-04-20 12:00
111         2013-04-20 12:30    2013-04-20 18:30
88          2013-04-20 11:00    2013-04-20 12:00
88          2013-04-20 12:30    2013-04-20 19:30
66          2013-04-20 10:00    2013-04-20 12:00
66          2013-04-20 12:30    2013-04-20 17:00

EmployeeID  Hours   WorkDate        CrewID(desired)
11          10.00   2013-04-18      13
88          8.00    2013-04-18      12
66          8.00    2013-04-18      12
111         8.00    2013-04-18      11
11          7.50    2013-04-20      13
88          8.00    2013-04-20      13
66          6.50    2013-04-20      12
111         8.00    2013-04-20      11

2 个答案:

答案 0 :(得分:1)

应该是一个简单的连接。

declare @tbl_Crew_Employees table(CrewID int, EmployeeID int, StartDate date, EndDate date)
insert into @tbl_Crew_Employees
values
(13,11,'2013-03-30','2013-05-12'),
(12,88,'2013-01-02','2013-04-18'),
(12,66,'2013-01-02','2013-06-30'),
(13,88,'2013-04-19','2013-04-21'),
(11,111,'2013-01-02','2013-04-28')

declare @tbl_TimeEntries table (EmployeeID int, StartDateTime datetime, EndDateTime datetime)
insert into @tbl_TimeEntries
values
(11,'2013-04-18 08:00','2013-04-18 12:00'),
(11,'2013-04-18 12:30','2013-04-18 18:30'),
(111,'2013-04-18 10:00','2013-04-18 12:00'),
(111,'2013-04-18 12:30','2013-04-18 18:30'),
(88,'2013-04-18 11:00','2013-04-18 12:00'),
(88,'2013-04-18 12:30','2013-04-18 19:30'),
(66,'2013-04-18 10:00','2013-04-18 12:00'),
(66,'2013-04-18 12:30','2013-04-18 18:30'),
(11,'2013-04-20 08:00','2013-04-20 12:00'),
(11,'2013-04-20 12:30','2013-04-20 18:00'),
(111,'2013-04-20 10:00','2013-04-20 12:00'),
(111,'2013-04-20 12:30','2013-04-20 18:30'),
(88,'2013-04-20 11:00','2013-04-20 12:00'),
(88,'2013-04-20 12:30','2013-04-20 19:30'),
(66,'2013-04-20 10:00','2013-04-20 12:00'),
(66,'2013-04-20 12:30','2013-04-20 17:00')

SELECT  
    t.EmployeeID, 
    c.CrewID,
        SUM(DATEDIFF(SECOND, t.StartDateTime, t.EndDateTime) / 60.0
                     / 60.0) ,
        CAST(t.StartDateTime AS date)
FROM    @tbl_TimeEntries t
INNER JOIN 
    @tbl_Crew_Employees c on 
    c.EmployeeID = t.EmployeeID
    and c.StartDate <= cast(t.StartDateTime as date)
    and c.EndDate >= cast(t.EndDateTime as date)
GROUP BY t.EmployeeID, CAST(t.StartDateTime AS date), c.CrewID
ORDER BY CAST(t.StartDateTime AS date)

答案 1 :(得分:1)

试试这个:

SELECT 
    tbl_TimeEntries.employeeid
    ,SUM(DATEDIFF(SECOND, StartDateTime, EndDateTime) / 60.0 / 60.0) AS HOURS
    ,CAST(StartDateTime AS DATE) AS WorkDate
    ,tbl_Crew_Employees.crewid
FROM tbl_TimeEntries 
INNER JOIN tbl_Crew_Employees ON tbl_timeentries.employeeid = tbl_Crew_Employees.employeeid 
    AND startdatetime >= startdate 
    AND enddatetime <= enddate
GROUP BY tbl_TimeEntries.employeeid
        ,tbl_Crew_Employees.crewid
        ,CAST(tbl_TimeEntries.StartDateTime AS DATE)
ORDER BY WorkDate