T-SQL:30 天间隔后的最新事件

发布于 2024-11-18 21:11:54 字数 656 浏览 2 评论 0原文

我需要查找最近 30 天或更长时间没有发生事件之后发生的所有事件。我当前的查询仅找到第一个 30 天的间隔。如果没有 30 天或更长的间隔,则我使用默认日期返回所有行。

我还应该小心,如果事件早于 30 天,则不要返回该事件的单个实例。

鉴于以下事件,最近 30 天间隔后的最近事件应该是 6/30/2011,但我的查询返回 4/13/2011

EventDate    EventType
=========    =========
4/13/2011    1
5/20/2011    1
6/30/2011    1

DECLARE @DefaultDate DATETIME 
SET @DefaultDate = '1/1/2011'

SELECT ISNULL(MAX(EventDate), @DefaultDate) 
FROM Events e 
WHERE 
    e.EventType = 1
    AND NOT EXISTS (SELECT 1 
                    FROM Events
                    WHERE  EventType = 1
                    AND DATEDIFF(dd,EventDate, e.EventDate) ) > 30
                   )

I need to find all the events occurring after the most recent gap of 30 days or more without an event. My current query only finds the first gap of 30 days. If there aren't any gaps of 30 days or more then I use a default date to return all the rows.

I should also be careful not to return a single instance of an event if that event is older that 30 days.

Given the following events, the most recent event after most recent 30 day gap should be 6/30/2011, but my query returns 4/13/2011

EventDate    EventType
=========    =========
4/13/2011    1
5/20/2011    1
6/30/2011    1

DECLARE @DefaultDate DATETIME 
SET @DefaultDate = '1/1/2011'

SELECT ISNULL(MAX(EventDate), @DefaultDate) 
FROM Events e 
WHERE 
    e.EventType = 1
    AND NOT EXISTS (SELECT 1 
                    FROM Events
                    WHERE  EventType = 1
                    AND DATEDIFF(dd,EventDate, e.EventDate) ) > 30
                   )

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

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

发布评论

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

评论(2

゛时过境迁 2024-11-25 21:11:55

试试这个:

declare @t table(EventDate datetime)

insert @t(EventDate) values('4/13/2011'), ('5/20/2011'), ('6/30/2011'), ('7/1/2011')

select *
from @t
where EventDate >
(
    select max(t1.EventDate)
    from @t t1
    join @t t2 ON t2.EventDate > t1.EventDate
        and not exists (
            select 1
            from @t t3
            where t3.EventDate < t2.EventDate and t3.EventDate > t1.EventDate
        )
    where datediff(day, t1.EventDate, t2.EventDate) > 30
)

Try this:

declare @t table(EventDate datetime)

insert @t(EventDate) values('4/13/2011'), ('5/20/2011'), ('6/30/2011'), ('7/1/2011')

select *
from @t
where EventDate >
(
    select max(t1.EventDate)
    from @t t1
    join @t t2 ON t2.EventDate > t1.EventDate
        and not exists (
            select 1
            from @t t3
            where t3.EventDate < t2.EventDate and t3.EventDate > t1.EventDate
        )
    where datediff(day, t1.EventDate, t2.EventDate) > 30
)
递刀给你 2024-11-25 21:11:55

专为您的 sql 而设计。修正了一个误会

SELECT COALESCE(t1.Eventdate, @DefaultDate) from event t1
RIGHT JOIN
(
SELECT max(EventDate) EventDate FROM event t 
WHERE EventType = 1 AND 
 NOT EXISTS 
(SELECT 1 FROM event WHERE EventType = 1 AND
t.eventdate <= eventdate + 30 and t.eventdate > eventdate)
AND EXISTS (SELECT 1 FROM event WHERE EventType = 1
 AND t.eventdate > eventdate)) t2
 on t1.eventdate >= t2.eventdate

Made to fit your sql. Fixed a misunderstanding

SELECT COALESCE(t1.Eventdate, @DefaultDate) from event t1
RIGHT JOIN
(
SELECT max(EventDate) EventDate FROM event t 
WHERE EventType = 1 AND 
 NOT EXISTS 
(SELECT 1 FROM event WHERE EventType = 1 AND
t.eventdate <= eventdate + 30 and t.eventdate > eventdate)
AND EXISTS (SELECT 1 FROM event WHERE EventType = 1
 AND t.eventdate > eventdate)) t2
 on t1.eventdate >= t2.eventdate
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文