Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/kubernetes/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 在目标表中两个日期之间的日期联接表_Sql Server_Tsql - Fatal编程技术网

Sql server 在目标表中两个日期之间的日期联接表

Sql server 在目标表中两个日期之间的日期联接表,sql-server,tsql,Sql Server,Tsql,我有两张桌子 tbl_TimeEntries EmployeeID int, StartDateTime datetime, EndDateTime datetime tbl_Crew_Employees CrewID, EmployeeID, StartDate, EndDate 我还有一个查询,可以生成每个员工每天的工作小时数,但我还想包括员工当天的工作人员 SELECT tbl_TimeEntries.EmployeeID,

我有两张桌子

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_员工使用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

应该是一个简单的连接

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)
declare@tbl\u Crew\u EmployeeID int、StartDate日期、EndDate日期)
插入@tbl_全体员工
价值观
(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表(EmployeeID int、StartDateTime datetime、EndDateTime datetime)
插入@tbl\u时间项
价值观
(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')
挑选
t、 雇员ID,
c、 克雷维德,
总和(DATEDIFF(秒,t.StartDateTime,t.EndDateTime)/60.0
/ 60.0) ,
演员阵容(t.StartDateTime为日期)
来自@tbl_timet
内连接
@tbl_机组人员_员工c接通
c、 EmployeeID=t.EmployeeID
c.StartDate=cast(t.EndDateTime为日期)
按t.EmployeeID分组,演员阵容(t.StartDateTime为日期),c.CrewID
按演员顺序(t.StartDateTime为日期)
试试这个:

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
选择
tbl_TimeEntries.employeeid
,总和(DATEDIFF(秒,StartDateTime,EndDateTime)/60.0/60.0)为小时
,铸造(起始日期为日期)为工作日期
,tbl_Crew_Employees.crewid
从tbl_时间条目
在tbl_timeentries.employeeid=tbl_Crew_employeeid上内部加入tbl_Crew_员工。employeeid
和startdatetime>=startdate

而enddatetime实际上,这是行不通的。加入还必须包括双方的日期。该员工在不同的日期在不同的班组工作,您的解决方案对此没有规定。基于缺少样本数据,我假设日期必须相等。请参见编辑。如果它们可以在几天之间流动,我们可以添加跨越逻辑——您只需要clarify@scsimon您有一些语法错误-是要更正还是应该更正?看起来您希望示例数据@Paul不重叠。。。请参见编辑。您能添加几行样本数据吗?另外,请显示您希望最终结果是什么样的。时间中心的一些测试日期如何?工作组之间的工作流程是否可以在同一天进行?一名工作组成员每天只能工作一个工作组