我有这个表,用于保存用户登录注销详细信息。 我想用一周的开始日查询数据,并每周获取数据, 例如,如果我从2016年3月1日到2016年3月28日查询并且我的周开始日是星期三,那么我每周需要所有员工数据 3月2日至8日 3月9日至15日 3月16日至22日 为每个员工。如果有人在那一周工作超过40小时,那么我还需要计算额外的小时数。
CREATE TABLE #tempWeekdatbefore
(EmployeeId BIGINT, LoginTime DATETIME, LogOutTime DATETIME)
INSERT INTO #tempWeekdatbefore (EmployeeId,LoginTime,LogOutTime)
Values
(83 ,'2016-03-09 06:55:22.000', '2016-03-09 14:29:11.000'),
(98 ,'2016-03-09 07:04:22.000', '2016-03-09 11:53:12.000'),
(84 ,'2016-03-09 07:07:06.000', '2016-03-09 13:39:03.000'),
(136 ,'2016-03-09 07:29:14.000', '2016-03-09 13:20:24.000'),
(420 ,'2016-03-09 07:33:48.000', '2016-03-09 11:42:52.000'),
(101102 ,'2016-03-09 07:49:25.000', '2016-03-09 11:42:53.000'),
(425 ,'2016-03-09 07:49:45.000', '2016-03-09 12:47:46.000'),
(100750 ,'2016-03-09 07:56:26.000', '2016-03-09 12:43:16.000'),
(90 ,'2016-03-09 08:03:51.000', '2016-03-09 11:19:38.000'),
(78 ,'2016-03-09 08:52:09.000', '2016-03-09 13:26:56.000'),
(89 ,'2016-03-09 09:06:57.000', '2016-03-09 13:41:30.000'),
(60600 ,'2016-03-09 09:31:17.000', '2016-03-09 17:13:48.000'),
(219 ,'2016-03-09 11:50:51.000', '2016-03-09 15:07:42.000'),
(90 ,'2016-03-09 11:54:38.000', '2016-03-09 16:47:30.000'),
(420 ,'2016-03-09 12:12:33.000', '2016-03-09 14:53:59.000'),
(101102 ,'2016-03-09 12:12:42.000', '2016-03-09 16:43:33.000'),
(337 ,'2016-03-09 12:17:22.000', '2016-03-09 15:54:39.000'),
(98 ,'2016-03-09 12:22:27.000', '2016-03-09 16:39:59.000'),
(425 ,'2016-03-09 13:08:09.000', '2016-03-09 17:00:16.000'),
(100750 ,'2016-03-09 13:09:32.000', '2016-03-09 15:00:00.000'),
(136 ,'2016-03-09 13:32:12.000', '2016-03-09 16:00:00.000'),
(84 ,'2016-03-09 13:50:37.000', '2016-03-09 16:23:04.000'),
(89 ,'2016-03-09 14:11:00.000', '2016-03-09 15:00:00.000'),
(101147 ,'2016-03-09 14:52:07.000', '2016-03-09 19:03:44.000'),
(81 ,'2016-03-09 14:54:05.000', '2016-03-09 19:08:47.000'),
(219 ,'2016-03-09 15:31:52.000', '2016-03-09 19:08:48.000'),
(100701 ,'2016-03-09 16:00:18.000', '2016-03-09 19:05:08.000'),
(337 ,'2016-03-09 16:27:44.000', '2016-03-09 19:08:50.000'),
(90 ,'2016-03-09 16:54:34.000', '2016-03-09 17:38:00.000')
SELECT EmployeeId
,SUM(CASE WHEN ISNULL(RegTime, 0)>40 THEN RegTime-40 ELSE 0 END) AS ExtraHour
,SUM(CASE WHEN ISNULL(RegTime, 0)>40 THEN 40 ELSE RegTime END) AS RegTime
FROM(
Select EmployeeId,
DATEPART(dw,LoginTime) AS WeekNumber,
DATEPART(MONTH,LoginTime) AS MonthNumber,
DATEPART(Year,LoginTime) AS YearNumber,
SUM(CONVERT(decimal(18, 2),
DATEDIFF(MINUTE, LoginTime, isnull( LogOutTime,getdate())) / 60.00)) AS RegTime
FROM #tempWeekdatbefore WITH(NOLOCK)
WHERE (convert(date, LoginTime) >= convert(date, '3/9/2016')) AND
( convert(date,LogOutTime) <= convert(date, '3/9/2016'))
GROUP BY EmployeeId, DATEPART(dw,LoginTime),
DATEPART(MONTH,LoginTime), DATEPART(Year,LoginTime)) as e
GROUP BY EmployeeId, WeekNumber, MonthNumber, YearNumber
答案 0 :(得分:0)
在构建BusinessObjects Universe(First day of previous week with a twist)时,我做了一些确定上一周,当前和下周的第一天和最后几天的工作,其中包含一周的变量开始日。
显然,BusinessObjects部分并不适用,但我仍然认为底层SQL可能仍然有用......
declare @StartDate datetime
declare @StartDateDayOfWeek int
declare @BeginningDayOfWeek int
set @StartDate = '03/01/2016'
set @StartDateDayOfWeek = DATEPART(dw, @StartDate)
set @BeginningDayOfWeek = 4 --Wednesday
select DATEADD(DAY, (-7 + ((@StartDateDayOfWeek - @BeginningDayOfWeek + 7) % 7)) * -1, @StartDate)
, DATEADD(DAY, (-13 + ((@StartDateDayOfWeek - @BeginningDayOfWeek + 7) % 7)) * -1, @StartDate)
, DATEADD(DAY, (-14 + ((@StartDateDayOfWeek - @BeginningDayOfWeek + 7) % 7)) * -1, @StartDate)
, DATEADD(DAY, (-20 + ((@StartDateDayOfWeek - @BeginningDayOfWeek + 7) % 7)) * -1, @StartDate)
, DATEADD(DAY, (-21 + ((@StartDateDayOfWeek - @BeginningDayOfWeek + 7) % 7)) * -1, @StartDate)
, DATEADD(DAY, (-27 + ((@StartDateDayOfWeek - @BeginningDayOfWeek + 7) % 7)) * -1, @StartDate)
如果复制该SQL并运行它,您将获得您提到的日期。你显然需要根据你的情况调整它,但希望这会让你朝着正确的方向前进。
诺尔