2017-08-09 63 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

相關問題