2017-08-09 54 views
1

我有两个表加入表上的日期两个日期之间

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范围,要么使用某种我不知道的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 
+0

能否请您添加样本数据的几行? – Eli

+0

此外,请显示您想要的最终结果。 – Eli

+0

对于TimeEntries – scsimon

回答

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 
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

实际上,这是行不通的。该加入还必须包括双方的日期。该员工在不同的日期使用不同的工作人员,您的解决方案不提供这一点。基于缺乏样本数据的 – Paul

+1

,那么我假设日期必须相等。请参阅编辑。如果它们可以在几天之间流动,我们可以添加生成逻辑 - 您只需要澄清 – scsimon

+0

@scsimon您有一些语法错误 - 您想更正它们,还是应该? – Eli

相关问题