请参阅下面的数据图片:
表格结构:
CREATE TABLE [dbo].[tblData](
[agentName] [nvarchar](255) NULL,
[DateTime] [datetime] NULL,
[loggedMins] [float] NULL,
[activeMinutes] [float] NULL,
[holdMinutes] [float] NULL,
[inactiveMinutes] [float] NULL
) ON [PRIMARY]
示例数据:
Insert Into TblData Values ('Doe, John', '01/21/2018 23:30:00', 30, 0, 0, 30)
Insert Into TblData Values ('Doe, John', '01/22/2018 00:00:00', 30, 0, 0, 30)
Insert Into TblData Values ('Doe, John', '01/22/2018 00:30:00', 30, 0.018, 0, 29.982)
Insert Into TblData Values ('Doe, John', '01/22/2018 01:00:00', 30, 0, 0, 29.982)
Insert Into TblData Values ('Doe, John', '01/22/2018 01:30:00', 30, 0, 0, 29.016)
Insert Into TblData Values ('Doe, John', '01/22/2018 02:00:00', 30, 0, 0, 0)
Insert Into TblData Values ('Doe, John', '01/22/2018 02:30:00', 30, 0, 0, 18.132)
Insert Into TblData Values ('Doe, John', '01/22/2018 03:00:00', 30, 0, 0, 30)
Insert Into TblData Values ('Doe, John', '01/22/2018 03:30:00', 30, 0, 0, 30)
Insert Into TblData Values ('Doe, John', '01/22/2018 04:00:00', 27.55, 0, 0, 27.55)
Insert Into TblData Values ('Doe, John', '01/22/2018 18:00:00', 19.1166666666667, 1.86578666666667, 0, 16.9660416666667)
Insert Into TblData Values ('Doe, John', '01/22/2018 18:30:00', 30, 0, 0, 30)
Insert Into TblData Values ('Doe, John', '01/22/2018 19:00:00', 30, 0, 0, 30)
Insert Into TblData Values ('Doe, John', '01/22/2018 19:30:00', 30, 0, 0, 26.532)
Insert Into TblData Values ('Doe, John', '01/22/2018 20:00:00', 30, 0, 0, 14.568)
Insert Into TblData Values ('Doe, John', '01/22/2018 20:30:00', 30, 0, 0, 30)
Insert Into TblData Values ('Doe, John', '01/22/2018 21:00:00', 30, 0, 0, 30)
Insert Into TblData Values ('Doe, John', '01/22/2018 21:30:00', 30, 0, 0, 11.232)
Insert Into TblData Values ('Doe, John', '01/22/2018 22:00:00', 30, 0, 0, 13.266)
Insert Into TblData Values ('Doe, John', '01/22/2018 22:30:00', 30, 0, 0, 30)
Insert Into TblData Values ('Doe, John', '01/22/2018 23:00:00', 30, 0, 0, 30)
Insert Into TblData Values ('Doe, John', '01/22/2018 23:30:00', 30, 0, 0, 16.551)
Insert Into TblData Values ('Doe, John', '01/23/2018 00:00:00', 30, 4.416, 0, 25.584)
Insert Into TblData Values ('Doe, John', '01/23/2018 00:30:00', 30, 0, 0, 30)
Insert Into TblData Values ('Doe, John', '01/23/2018 01:00:00', 30, 0, 0, 30)
Insert Into TblData Values ('Doe, John', '01/23/2018 01:30:00', 30, 0, 0, 30)
Insert Into TblData Values ('Doe, John', '01/23/2018 18:00:00', 30, 0, 0, 30)
Insert Into TblData Values ('Doe, John', '01/23/2018 18:30:00', 30, 0, 0, 30)
我的问题是我需要根据' DateTime'来计算工作日期。柱。 John Doe工作时间为下午6点至凌晨4点。需要将工作日期添加到新列,该列应等于6PM日期。以下是我的逻辑:
我很难翻译成代码:(帮助!
答案 0 :(得分:1)
Matther Baker的回答非常好!我按agentName和time排序行;然后将agentName添加到代码中,以便每个代理的时间分别计算:
--CREATING A TEMP TABLE
Select
row_number() over(order by agentName, [DateTime] asc) as [Rnk], *
Into ##TmpData
from TblData
Order by agentName, [DateTime] asc
--ACTUAL LOGIC
SELECT *,
(SELECT MIN(DateTime) FROM tblData S
WHERE S.DateTime <= M.DateTime
AND S.DateTime > DATEADD(HOUR, -15, M.DateTime) and S.agentName = M.agentName
) ShiftStart
FROM ##TmpData M
--DROPPING TEMP TABLE
Drop Table ##TmpData
我看到没有人做过超过13个小时的转变,因此增加了缓冲区并将时间间隔改为15个。
非常感谢你们!
答案 1 :(得分:0)
看看这个。我将日期时间仅限于时间,并设置中午的支点,如果时间是中午之后,那么今天。如果它在中午之前那么它的昨天。如果是早期开始/晚期结束等,我选择正午。
SELECT *
, CASE WHEN CAST(DateTime AS TIME) > '12:00:00' THEN CAST(DateTime AS DATE)
ELSE DATEADD(DAY, -1, CAST(DateTime AS DATE))
END AS WorkDate
FROM @tblData
这将为您提供一个可以分组的列。也许把它包装成cte来帮助。
答案 2 :(得分:0)
第二个回答:
SELECT *
, (SELECT MIN(DateTime)
FROM tblData S
WHERE S.DateTime <= M.DateTime
AND S.DateTime > DATEADD(HOUR, -12, M.DateTime)
) ShiftStart
FROM tblData M
可能是你最好的替代方案 - 这可以解释为一个班次不会超过12小时,但你可以调整一下。也不是最有效的方法。