【问题标题】:SQL How to get all the repeats within a specified timeframeSQL如何在指定的时间范围内获取所有重复
【发布时间】:2010-11-23 20:18:44
【问题描述】:

我的桌子看起来像

int callid not null,
datetime segstart not null,
varchar calling_pty not null

我想要得到的是所有具有相同 call_pty 的行,在 5 分钟内每天出现一次以上。

我好难过。我已经看过 TOP 1、datediff 和 select next 和 previous row 示例,但我无法解决这个问题。

我使用的是 MS SQL 2005。

谢谢!

【问题讨论】:

    标签: sql database sql-server-2005


    【解决方案1】:
    select t1.callid, t1.segstart, t1.calling_pty
    from MyTable t1
    inner join MyTable t2 on t1.calling_pty = t2.calling_pty 
        and t1.segstart < t2.segstart
    where datediff(mi, t1.segstart, t2.segstart) <= 5 --a difference of 5 minutes 59 secs. still returns 5
    

    请注意,由于DATEDIFF 计算跨越的日期边界的数量,因此在计算分钟时它可能有点近似。为了获得更好的准确性,您可能希望使用

    where datediff(s, t1.segstart, t2.segstart) <= 300 --this looks at difference in seconds, so much nmore accurate
    

    【讨论】:

    • 太棒了,这几乎可以工作!奇怪的是第一个结果也显示为最后一个,所以它是重复的。
    • @CodingIsAwesome:尝试将&lt;&gt; 更改为&lt;
    猜你喜欢
    • 2020-12-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-20
    • 2013-09-12
    • 1970-01-01
    • 2019-04-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多