gpt4 book ai didi

sql-server - 连续 5 周获取一周内有另一条记录的记录

转载 作者:行者123 更新时间:2023-12-04 04:49:46 25 4
gpt4 key购买 nike

所以我在办公室有一张用户表,他们从一天开始,在同一天或晚些时候结束。我需要找到用户至少连续 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')

最佳答案

这是一个如何使用 Recursive CTE 完成此操作的简单示例声明。

我不确定您究竟需要什么作为“输出”,所以我正在显示用户及其上周的开始和结束日期。您可以自由地重新设计它以满足您的需求:

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-04'),
(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')

;WITH DataSource ([UserId], [OfficeId], [Start], [End], [Level]) AS
(
SELECT AnchorMember.[UserId]
,AnchorMember.[OfficeId]
,DATEADD(DAY, -(DATEPART(WEEKDAY, AnchorMember.[Start])-1), AnchorMember.[Start])
,DATEADD(DAY, 7-(DATEPART(WEEKDAY, AnchorMember.[End])), AnchorMember.[End])
,1 AS [Level]
FROM @visits AS AnchorMember
UNION ALL
SELECT RecursiveMember.[UserId]
,RecursiveMember.[OfficeId]
,DATEADD(DAY, -(DATEPART(WEEKDAY, RecursiveMember.[Start])-1), RecursiveMember.[Start])
,DATEADD(DAY, 7-(DATEPART(WEEKDAY, RecursiveMember.[End])), RecursiveMember.[End])
,DS.[Level] + 1
FROM @visits AS RecursiveMember
INNER JOIN DataSource DS
ON RecursiveMember.[UserId] = DS.[UserId]
AND RecursiveMember.[OfficeId] = DS.[OfficeId]
-- This is the important part: The "Week StartDate" + 1 day should be eaual to previous "Week EndDate"
WHERE DATEADD(DAY, -(DATEPART(WEEKDAY, RecursiveMember.[End])-1), RecursiveMember.[End]) = DATEADD(DAY, 8-(DATEPART(WEEKDAY, DS.[Start])), DS.[Start])
)
SELECT [UserId]
,[OfficeId]
,[Start]
,[End]
FROM DataSource
WHERE [Level] = 5
ORDER BY [UserId]
,[OfficeId]
,[Start]
,[End]

我们在表达式中所做的事情可以用以下步骤来描述:
  • 选择所有记录并定义它们的明星和结束 WEEK 天。
  • 对于上面的每条记录,检查是否还有其他相同用户 ID 和办公室 ID 的记录,但从下周开始。如果存在这样的记录,则返回它。
  • 做第 1 步,但对于第 2 步的结果,直到在第 2 步中找不到记录

  • [级别] 列显示当前记录之前的连续周数。因此,如果您需要它工作 12 周,请将最后的“WHERE”子句中的“5”替换为“12”。

    编辑:

    由于您只需要至少一组连续周的不同用户,我们可以按如下方式减少列数:
    ;WITH DataSource ([UserId], [OfficeId], [Start], [Level]) AS
    (
    SELECT AnchorMember.[UserId]
    ,AnchorMember.[OfficeId]
    ,DATEADD(DAY, -(DATEPART(WEEKDAY, AnchorMember.[Start])-1), AnchorMember.[Start])
    ,1 AS [Level]
    FROM @visits AS AnchorMember
    UNION ALL
    SELECT RecursiveMember.[UserId]
    ,RecursiveMember.[OfficeId]
    ,DATEADD(DAY, -(DATEPART(WEEKDAY, RecursiveMember.[Start])-1), RecursiveMember.[Start])
    ,DS.[Level] + 1
    FROM @visits AS RecursiveMember
    INNER JOIN DataSource DS
    ON RecursiveMember.[UserId] = DS.[UserId]
    AND RecursiveMember.[OfficeId] = DS.[OfficeId]
    WHERE DATEADD(DAY, -(DATEPART(WEEKDAY, RecursiveMember.[Start])-1), RecursiveMember.[Start]) = DATEADD(DAY, 7, DS.[Start])
    )
    SELECT DISTINCT [UserId]
    ,[OfficeId]
    FROM DataSource
    WHERE [Level] = 5
    ORDER BY [UserId]
    ,[OfficeId]

    由于我无法访问您的数据,我无法确定是什么导致了延迟,因此这无济于事。

    如果在添加 group 子句之前查询性能良好,则可以尝试将 CTE 的结果插入到临时表或表变量中,然后对其进行“分组”。

    如果这是解决性能问题,您可以发布查询执行计划。

    关于sql-server - 连续 5 周获取一周内有另一条记录的记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17613099/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com