如何按日期范围进行分组?

发布于 2024-12-08 16:56:20 字数 890 浏览 0 评论 0原文

考虑这个表结构。

 Key       ID       VISITDATE
 1         1        2011-01-07
 2         1        2011-01-09
 3         2        2011-01-10
 4         1        2011-01-12
 5         3        2011-01-12
 6         1        2011-01-15
 7         2        2011-01-21
 9         1        2011-02-28
 10        2        2011-03-21
 11        1        2011-01-06

我需要获取所有 ID、Key、min(VisitDate),其中 VisitDate 在 10 天内?如果您在 10 天内有两次访问,则结果中需要出现一行。

结果

 KEY      ID        VISITDATE
 11        1         2011-01-06
 3         2         2011-01-10
 5         3         2011-01-12
 7         2         2011-01-21
 9         1         2011-02-28
10         2         2011-03-21

可以在没有自连接的情况下完成此操作吗?我有一个查询,它与 ID 上的表进行自连接并检查 datediff。有更好的解决方案吗?我们可以在这里使用递归 CTE 吗?

编辑

更喜欢可以使用日期列索引的解决方案

Conside this Table Structure.

 Key       ID       VISITDATE
 1         1        2011-01-07
 2         1        2011-01-09
 3         2        2011-01-10
 4         1        2011-01-12
 5         3        2011-01-12
 6         1        2011-01-15
 7         2        2011-01-21
 9         1        2011-02-28
 10        2        2011-03-21
 11        1        2011-01-06

I need to get all the IDs,Key,min(VisitDate) where VisitDate is within 10 days span?if you have two visits within 10 days one row need to be there in the result.

Result

 KEY      ID        VISITDATE
 11        1         2011-01-06
 3         2         2011-01-10
 5         3         2011-01-12
 7         2         2011-01-21
 9         1         2011-02-28
10         2         2011-03-21

Can this be done without a self join. i have a query which does a self join with the table on ID and check the datediff.is there a better solution?can we use recursive CTE here?

EDIT

Prefer a solution which can use the index on date column

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倾`听者〃 2024-12-15 16:56:20

是的,CTE 可以很好地解决这个问题(最近我的一切都是 CTE)......

;WITH TenDayVisits
AS (

SELECT 
        ID
        ,MIN(VisitDate) AS VisitDate
    FROM Visits
    GROUP BY ID
    UNION ALL
    SELECT
        t.ID
        ,v.VisitDate
    FROM Visits AS v
    JOIN TenDayVisits AS t ON v.ID = t.ID               
        AND DATEDIFF(dd,t.Visitdate,v.VisitDate) > 10
)

SELECT
    DISTINCT  
    v.[key]
    ,t.id
    ,t.VisitDate
FROM TenDayVisits as T
JOIN Visits AS v ON t.id = v.id
    AND t.VisitDate = v.VisitDate

Yes a CTE would work nicely for this (everything with me is CTEs lately)...

;WITH TenDayVisits
AS (

SELECT 
        ID
        ,MIN(VisitDate) AS VisitDate
    FROM Visits
    GROUP BY ID
    UNION ALL
    SELECT
        t.ID
        ,v.VisitDate
    FROM Visits AS v
    JOIN TenDayVisits AS t ON v.ID = t.ID               
        AND DATEDIFF(dd,t.Visitdate,v.VisitDate) > 10
)

SELECT
    DISTINCT  
    v.[key]
    ,t.id
    ,t.VisitDate
FROM TenDayVisits as T
JOIN Visits AS v ON t.id = v.id
    AND t.VisitDate = v.VisitDate
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文