前后行

时间:2016-10-19 10:48:33

标签: sql sql-server

我需要一个SQL查询,它从具有连续状态数据的表中提取数据,如下所示。如果我想获得给定时间窗口的数据说一小时/天,那么它应该过滤掉记录并转动状态信息。

过滤:FROM' 2016-08-11 23:50:00.000' TO' 2016-08-13 01:15:00.000' - 85分钟,以黄色突出显示 -

过滤器如下 -

当我提供以下过滤器时,

DECLARE @StartDate DATETIME = '2016-08-11 23:50:00.000'
DECLARE @EndDate   DATETIME = '2016-08-15 01:15:00.000' 

理想情况下,它应将时间窗视为 -

2016-08-11 23:50:00 to 2016-08-12 01:15:00 - 85 minutes
2016-08-12 23:50:00 to 2016-08-13 01:15:00 - 85 minutes
2016-08-13 23:50:00 to 2016-08-14 01:15:00 - 85 minutes
2016-08-14 23:50:00 to 2016-08-15 01:15:00 - 85 minutes
2016-08-15 23:50:00 to 2016-08-16 01:15:00 - 85 minutes

依旧......

更新3

enter image description here

有人可以帮我解决这个问题吗?

示例数据 -

create table #temp1 ([State] varchar(20),StartTimeStamp Datetime2,    EndTimeStamp Datetime2, DurationInSeconds int)
Insert into #temp1 values('Away',       '2016-08-11 23:40:00.000000',           '2016-08-11 23:45:00.000000',   300     )
,('Appear Away','2016-08-11 23:45:00.000000',           '2016-08-11 23:50:00.000000',   300 )
,('Available', '2016-08-11 23:50:00.000000',            '2016-08-11 23:55:00.000000',   300 )
,('Available','2016-08-11 23:55:00.000000',             '2016-08-11 23:59:59.000000',   299 )
,('Away',       '2016-08-12 00:00:00.000000',           '2016-08-12 00:05:00.000000',   300 )
,('Offline',    '2016-08-12 00:05:00.000000',           '2016-08-12 00:15:00.000000',   600 )
,('Away',       '2016-08-12 00:15:00.000000',           '2016-08-12 00:30:00.000000',   900 )
,('Appear Away','2016-08-12 00:30:00.000000',           '2016-08-12 01:15:00.000000',   2700 )
,('Away',       '2016-08-12 01:15:00.000000',           '2016-08-12 01:30:00.000000',   900 )
,('Offline',    '2016-08-12 01:30:00.000000',           '2016-08-12  18:30:00.000000',   64800 )
,('Appear Away','2016-08-12 18:30:00.000000',           '2016-08-12   23:30:00.000000',   18000 )
,('Available',  '2016-08-12 23:30:00.000000',           '2016-08-12 23:45:00.000000',   900 )
,('Away',       '2016-08-12 23:45:00.000000',           '2016-08-12 23:50:00.000000',   300 )
,('Offline',    '2016-08-12 23:50:00.000000',           '2016-08-12 23:55:00.000000',   300 )
,('Available',  '2016-08-12 23:55:00.000000',           '2016-08-12 23:59:59.000000',   299 )
,('Away',       '2016-08-13 00:00:00.000000',           '2016-08-13 00:05:00.000000',   300 )
,('Offline',    '2016-08-13 00:05:00.000000',           '2016-08-13 00:15:00.000000',   600 )
,('Away',       '2016-08-13 00:15:00.000000',           '2016-08-13 00:30:00.000000',   900 )
,('Appear Away','2016-08-13 00:30:00.000000',           '2016-08-13 01:15:00.000000',   2700 )
,('Away',       '2016-08-13 01:15:00.000000',           '2016-08-13 01:30:00.000000',   900 )

UPDATE2:预期输出:

enter image description here

2 个答案:

答案 0 :(得分:2)

您可以如下所示:

[4]

结果:

DECLARE @StartDate DATETIME = '2016-08-8 17:00:00.000'
DECLARE @EndDate DATETIME = '2016-08-15 18:00:00.000' 

;WITH TmpCte
AS
(
    SELECT
        T.*,
        IIF(T.StartTimeStamp < DATEADD(ms, DATEDIFF(ms, '00:00:00', CAST(@StartDate AS TIME)), CAST(CAST(T.StartTimeStamp AS DATE) AS DATETIME)), 
                DATEADD(ms, DATEDIFF(ms, '00:00:00', CAST(@StartDate AS TIME)), CAST(CAST(T.StartTimeStamp AS DATE) AS DATETIME)), 
                T.StartTimeStamp) AS TmpStart,
        IIF(T.EndTimeStamp > DATEADD(ms, DATEDIFF(ms, '00:00:00', CAST(@EndDate AS TIME)), CAST(CAST(T.EndTimeStamp AS DATE) AS DATETIME)),
                DATEADD(ms, DATEDIFF(ms, '00:00:00', CAST(@EndDate AS TIME)), CAST(CAST(T.EndTimeStamp AS DATE) AS DATETIME)), 
                T.EndTimeStamp) AS TmpEnd        
    FROM
        #temp T
), CTE as (
    SELECT * FROM 
    (
          SELECT
            *,
            ABS(DATEDIFF(SECOND, TmpStart, TmpEnd)) TmpDuration
          FROM TmpCte
          where 
            TmpStart >= @StartDate and 
            TmpEnd <=  @EndDate AND
            TmpEnd > TmpStart       
    ) A
    PIVOT(SUM(TmpDuration)
    FOR state IN ([Appear Away],[Available],[Away],[Offline],[online])  ) as     Pivottable

  )
  select CAST(StartTimestamp as Date) as LocalDate,SUM([Available]) as [Available] , SUM([Away]) as Away,SUM([Online]) as [Online],
  SUM(Offline) as [Offline] 
  from CTE 
  group by CAST(StartTimestamp as Date) 

答案 1 :(得分:0)

尝试如下。

DECLARE @StartDate DATETIME = '2016-08-11 23:45:00.000'
DECLARE @EndDate DATETIME = '2016-08-12 01:00:00.000' 

;WITH TmpCte
AS
(
    SELECT
        T.*,
        IIF(T.StartTimeStamp < @StartDate, 
                @StartDate,
                T.StartTimeStamp) AS TmpStart,
        IIF(T.EndTimeStamp > @EndDate,              
              @EndDate,
                T.EndTimeStamp) AS TmpEnd        
    FROM
        #temp T
), CTE as (
    SELECT * FROM 
    (
          SELECT
            *,
            ABS(DATEDIFF(SECOND, TmpStart, TmpEnd)) TmpDuration
          FROM TmpCte
          where 
            TmpStart >= @StartDate and 
            TmpEnd <=  @EndDate AND
            TmpEnd > TmpStart       
    ) A
    PIVOT(SUM(TmpDuration)
    FOR state IN ([Appear Away],[Available],[Away],[Offline],[online])  ) as     Pivottable

  )
  SELECT  
    CAST(StartTimestamp as Date) as LocalDate,
    SUM([Appear Away]) as [Appear Away], 
    SUM([Available]) as [Available] , 
    SUM([Away]) as Away,SUM([Online]) as [Online],
    SUM(Offline) as [Offline] 
  from CTE 
  group by CAST(StartTimestamp as Date)