【问题标题】:How to find the difference between two dates in the same column in SQL Server?如何在 SQL Server 的同一列中查找两个日期之间的差异?
【发布时间】:2012-08-21 08:40:13
【问题描述】:

我需要一个查询来找出两个日期之间的差异,

我的表格数据是这样的:

select  
    ROW_NUMBER() OVER (ORDER BY eventtime) as id,
    Eventcode, eventtime, status 
from cfw.dbo.DCTBLEVENTINFO 
where
   MeterID = 4722
   and EventTime >= '2011-10-21' and EventCode = 13 

数据:

id  Eventcode eventtime                status
1   13        2011-10-21 21:42:00.000   1
2   13        2011-10-21 22:02:24.107   0
3   13        2011-10-22 09:45:00.000   1
4   13        2011-10-22 10:05:24.107   0
5   13        2011-10-22 15:08:00.000   0
6   13        2011-10-22 16:33:00.000   1
7   13        2011-10-22 16:44:00.000   0
8   13        2011-10-22 16:53:24.107   0
9   13        2011-10-23 08:52:00.000   1
10  13        2011-10-23 09:12:24.107   0
11  13        2011-10-23 10:18:00.000   0
12  13        2011-10-23 10:35:00.000   1
13  13        2011-10-23 10:36:00.000   0
14  13        2011-10-23 10:55:24.107   0
15  13        2011-10-23 10:56:00.000   1
16  13        2011-10-23 11:01:00.000   0
17  13        2011-10-23 11:16:24.107   0
18  13        2011-10-23 18:28:00.000   1
19  13        2011-10-23 18:30:00.000   0

在此状态列中,1 指的是occurencetime0 指的是restorationtime。我必须取第一个发生时间和下一个相应恢复时间之间的差值,然后取下一个发生时间

前任

对于 2011-10-22 的日期,总共有 5 个条目,我必须将 occurencetime 作为 2011-10-22 09:45:00.000restoration time 作为 2011-10-22 10:05:24.107 并有所作为。下一个条目有 restorationtime alone(status 0) 我必须离开那个条目并搜索下一个 1 & 0 条目。然后有所作为。

我的最终结果集是这样的:

Eventcode   occurencetime            restorationtime              difference (sec)
13          2011-10-21 21:42:00.000  2011-10-21 22:02:24.107      1224
13          2011-10-22 09:45:00.000  2011-10-22 10:05:24.107      1224 
13          2011-10-22 16:33:00.000  2011-10-22 16:44:00.000       660

帮我做。

【问题讨论】:

    标签: sql-server sql-server-2008


    【解决方案1】:

    试试这个:

     ;with cte as (
     select ROW_NUMBER() OVER (ORDER BY eventtime) as id,
            Eventcode, eventtime, status 
    from cfw.dbo.DCTBLEVENTINFO 
    where MeterID = 4722
    and EventTime >= '2011-10-21' and EventCode = 13)
    select   c1.Eventcode,
             c1.eventtime as occurencetime,
             min(c2.eventtime) as restorationtime,
             datediff(ss,c1.eventtime,min(c2.eventtime)) as difference
    from     cte c1
    left outer join cte c2
    on       c1.eventtime < c2.eventtime           
    and      c1.Eventcode=c2.Eventcode
    where    c1.status=1
    and      c2.status=0      
    group by c1.Eventcode,c1.eventtime
    

    【讨论】:

      【解决方案2】:

      使用status = 1 获取所有行,然后使用top(1) ... order by 执行cross apply 以使用status = 0 获取下一行。

      select T1.EventCode,
             T1.EventTime as OccurenceTime,
             T3.RestorationTime,
             datediff(second, T1.EventTime, T3.RestorationTime) as "Difference(sec)"
      from cfw.dbo.DCTBLEVENTINFO as T1
        cross apply (
                    select top(1) T2.EventTime as RestorationTime
                    from cfw.dbo.DCTBLEVENTINFO as T2
                    where T2.EventTime > T1.EventTime and
                          T2.Status = 0 and
                          T2.MeterID = T1.MeterID and
                          T2.EventCode = T1.EventCode               
                    order by T2.EventTime
                    ) as T3
      where T1.Status = 1 and
            T1.MeterID = 4722 and
            T1.EventTime >= '20111021' and
            T1.EventCode = 13 
      

      结果:

      EventCode   OccurenceTime           RestorationTime         Difference(sec)
      ----------- ----------------------- ----------------------- ---------------
      13          2011-10-21 21:42:00.000 2011-10-21 22:02:24.107 1224
      13          2011-10-22 09:45:00.000 2011-10-22 10:05:24.107 1224
      13          2011-10-22 16:33:00.000 2011-10-22 16:44:00.000 660
      13          2011-10-23 08:52:00.000 2011-10-23 09:12:24.107 1224
      13          2011-10-23 10:35:00.000 2011-10-23 10:36:00.000 60
      13          2011-10-23 10:56:00.000 2011-10-23 11:01:00.000 300
      13          2011-10-23 18:28:00.000 2011-10-23 18:30:00.000 120
      

      【讨论】:

        猜你喜欢
        • 2013-03-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-08-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多