【问题标题】:SQL question: Getting records based on datediff from record to recordSQL问题:根据datediff从记录到记录获取记录
【发布时间】:2009-07-15 21:43:48
【问题描述】:

好的,这里有一个棘手的问题...如果我的数据如下所示:

表1

ID  Date_Created 
1   1/1/2009
2   1/3/2009
3   1/5/2009
4   1/10/2009
5   1/15/2009
6   1/16/2009

如何获取相隔 2 天的记录?我的最终结果集应该是第 1-3 行和第 5-6 行。谢谢!

【问题讨论】:

  • 为什么第 5-6 行、第 5 行和第 6 行仅相隔 1 天?
  • 对不起,我没有澄清......我的意思是相隔 2 天内。 2 是一个参数。可能是 3 或 4 天或其他任何时间。

标签: sql datediff


【解决方案1】:
SELECT l.*
FROM Table1 l
INNER JOIN Table1 r ON DATEDIFF(d, l.Date_Created, r.Date_Created) = 2
      AND r.Date_Created = (SELECT TOP 1 * FROM Table1 WHERE Date_Created > l.Date_Created ORDER BY Date_Create)

【讨论】:

    【解决方案2】:
    select distinct t1.*
    from Table1 t1
    inner join Table1 t2 
        on abs(cast(t1.Date_Created - t2.Date_Created as float)) between 1 and 2
    

    【讨论】:

    • 除非您希望每对匹配的 ID 出现两次(例如 (1,2) 和 (2,1)),否则您不应该在其中包含 abs()
    • 错了,必须的,比如获取第一条记录。
    【解决方案3】:

    -- 这给了你什么?

    select DISTINCT t1.id, t1.date_created, t2.id, t2.date_created from table1 t1, table1 t2 where datediff(dd,t1.date_created,t2.date_created) = 2 AND t1.id != t2.id按 t1.id 排序;

    【讨论】:

      【解决方案4】:

      这行得通吗?

      select t1.id, t2.id 
        from table1 t1 
        join table1 t2 
          on t2.date_created - t1.date_created <= 2
      

      【讨论】:

      • 几乎,但不完全。如果他们之间有记录,仍然可以匹配。虽然也许这就是他想要的?
      • 从现在开始的第 4、6、8 天呢?
      • @Joel:你是如何从他只想要连续记录(按 id)的问题中推断出来的?
      • @Jon:问题不是“从现在开始”,而是要找到最多相隔这么多天的(成对)记录,例如2.
      【解决方案5】:

      我可能会建议使用编程代码来做到这一点。您想要收集行组(单独的组)。我认为您无法使用单个查询来解决此问题(这只会给您返回一组行)。

      【讨论】:

      • 另外,不要让“2009 年 1 月 1 日”日期欺骗您认为您不需要 > your_start_date(如果开始日期甚至适用)。
      • 返回多个结果集的存储过程可能会为您完成此任务(我认为使用 sql server 存储过程可以实现多个结果集)。
      【解决方案6】:

      如果你想得到相隔“N”天的行,你可以试试这个:

      select t1.date_created, t2.date_created 
      from table1 t1, table1 t2 
      where t1.id <> t2.id and 
            t2.date_created-t1.date_created between 0 and N;
      

      例如,正如您所说,如果您想获取 2 天内的行, 您可以使用以下内容:

      select t1.date_created,t2.date_created 
      from table1 t1, table1.t2 
      where t1.id <> t2.id and 
            t2.date_created-t1.date_created between 0 and 2;
      

      我希望这会有所帮助....

      问候, 斯里克里希纳。

      【讨论】:

        【解决方案7】:

        游标是最快的,但这里有一个 SELECT 查询可以做到这一点。请注意,对于相隔“最多 N”天而不是 2 天,您必须将表 2 替换为从 0 到 N-1 的整数表(效率会变差)。

        我承认你想要什么并不完全清楚,但我猜你想要包含至少两行的行范围,其中连续的行最多相隔 2 天。如果日期与 ID 一起增加,这应该有效。

        with Two as (
          select 0 as offset union all select 1 
        ), r2(ID, Date_Created_o, dr) as (
          select
            ID, Date_Created+offset,
            Date_Created + offset - dense_rank() over (
              order by Date_Created+offset
            ) from r cross join Two
        )
          select
            min(ID) as start, max(ID) as finish
          from r2
          group by dr
          having min(ID) < max(ID)
          order by dr;
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多