所以我在办公室有一张用户表,他们从一天开始,并在同一天或晚些时候结束。我需要找到用户连续 5 周至少去过一次办公室的位置,例如,用户 1 在过去 5 周内去过办公室 1。
这是我正在使用的一些示例数据:
DECLARE @visits table
(
UserId int,
OfficeId int,
Start datetime,
[End] datetime
)
INSERT INTO @visits (UserId, OfficeId, Start, [End])
VALUES (1, 1, '2013-07-11', '2013-07-13'),
(1, 1, '2013-07-02', '2013-07-03'),
(1, 1, '2013-06-26', '2013-06-28'),
(1, 2, '2013-06-19', '2013-06-19'),
(1, 1, '2013-06-17', '2013-06-17'),
(1, 1, '2013-06-13', '2013-06-13'),
(2, 1, '2013-07-09', '2013-07-10'),
(2, 1, '2013-07-01', '2013-07-02'),
(2, 1, '2013-06-27', '2013-06-28'),
(2, 1, '2013-06-13', '2013-06-14'),
(2, 1, '2013-06-04', '2013-06-04')
我应该只取回 UserId 1,因为他已经在办公室 1 工作了 5 周,用户 2 错过了一周,所以不应该回来。
这需要工作 12 周,但为了简单起见,我选择了 5 个。
到目前为止,我几乎已经有了这个,我只需要一种按连续周分组的方法,然后添加 Count > 4
SELECT *,
dateadd(day, -datepart(dw, V1.Start) + 1, V1.Start) MondayOfStart,
DATEADD(day, 7 - DATEPART(dw, V1.[End]), V1.[End]) as SundayOfEnd,
DATEADD(day, -1, dateadd(day, -datepart(dw, V1.Start) + 1, V1.Start)) StartLess1
FROM @visits V1
INNER JOIN @visits V2 ON
V2.UserId = V1.UserId AND
V2.OfficeId = V1.OfficeId AND
DATEADD(day, 7 - DATEPART(dw, V2.[End]), V2.[End]) = DATEADD(day, -1, dateadd(day, -datepart(dw, V1.Start) + 1, V1.Start))
编辑:我要测试的一些实际数据:
VALUES
(2777, 2248, '2013-05-23 00:00:00.000', '2013-05-23 00:00:00.000'),
(2777, 2248, '2013-05-24 00:00:00.000', '2013-05-24 00:00:00.000'),
(2777, 2248, '2013-05-27 00:00:00.000', '2013-05-27 00:00:00.000'),
(2777, 2248, '2013-05-28 00:00:00.000', '2013-05-28 00:00:00.000'),
(2777, 2248, '2013-05-29 00:00:00.000', '2013-05-29 00:00:00.000'),
(2777, 2248, '2013-05-30 00:00:00.000', '2013-05-30 00:00:00.000'),
(2777, 2248, '2013-05-31 00:00:00.000', '2013-05-31 00:00:00.000'),
(2777, 2248, '2013-06-03 00:00:00.000', '2013-06-03 00:00:00.000'),
(2777, 2248, '2013-06-04 00:00:00.000', '2013-06-04 00:00:00.000'),
(2777, 2248, '2013-06-05 00:00:00.000', '2013-06-05 00:00:00.000'),
(2777, 2248, '2013-06-06 00:00:00.000', '2013-06-06 00:00:00.000'),
(2777, 2248, '2013-06-07 00:00:00.000', '2013-06-07 00:00:00.000'),
(2777, 2248, '2013-06-10 00:00:00.000', '2013-06-10 00:00:00.000'),
(2777, 2248, '2013-06-11 00:00:00.000', '2013-06-11 00:00:00.000'),
(2777, 2248, '2013-06-12 00:00:00.000', '2013-06-12 00:00:00.000'),
(2777, 2248, '2013-06-13 00:00:00.000', '2013-06-13 00:00:00.000'),
(2777, 2248, '2013-06-14 00:00:00.000', '2013-06-14 00:00:00.000'),
(2777, 2248, '2013-06-17 00:00:00.000', '2013-06-17 00:00:00.000'),
(2777, 2248, '2013-06-18 00:00:00.000', '2013-06-18 00:00:00.000'),
(2777, 2248, '2013-06-19 00:00:00.000', '2013-06-19 00:00:00.000'),
(2777, 2248, '2013-06-20 00:00:00.000', '2013-06-20 00:00:00.000'),
(2777, 2248, '2013-06-21 00:00:00.000', '2013-06-21 00:00:00.000'),
(2777, 2248, '2013-06-24 00:00:00.000', '2013-06-24 00:00:00.000'),
(2777, 2248, '2013-06-25 00:00:00.000', '2013-06-25 00:00:00.000'),
(2777, 2248, '2013-06-26 00:00:00.000', '2013-06-26 00:00:00.000'),
(2777, 2248, '2013-06-27 00:00:00.000', '2013-06-27 00:00:00.000'),
(2777, 2248, '2013-06-28 00:00:00.000', '2013-06-28 00:00:00.000')