SQL Query Compare values in per 15 minutes and display the result per hour

后端 未结 3 532
感动是毒
感动是毒 2021-01-24 02:04

I have a table with 2 columns. UTCTime and Values. The UTCTime is in 15 mins increment. I want a query that would compare the value to the previous value in one hour span and di

3条回答
  •  伪装坚强ぢ
    2021-01-24 03:09

    Mine isn't much different from Vasanth's, same idea different approach.

    The idea is that you need recursion to carry it out simply. You could also use the LEAD() function to look at rows ahead of your current row, but in this case that would require a big case statement to cover every outcome.

    ;WITH T
    AS (
            SELECT a.UTCTime,b.VALUE,ROW_NUMBER() OVER(PARTITION BY a.UTCTime ORDER BY b.UTCTime DESC)'RowRank'
            FROM (SELECT * 
                  FROM  #Table1 
                  WHERE DATEPART(MINUTE,UTCTime) = 0
                  )a
            JOIN #Table1 b
               ON b.UTCTIME BETWEEN a.UTCTIME AND DATEADD(hour,1,a.UTCTIME)
       )
     SELECT T.UTCTime, SUM(CASE WHEN T.Value = T2.Value THEN 1 ELSE 0 END)
     FROM T 
     JOIN T T2
       ON T.UTCTime = T2.UTCTime 
        AND T.RowRank = T2.RowRank -1
     GROUP BY T.UTCTime
    

    If you run the portion inside the ;WITH T AS ( ) you'll see that gets us the hour we're looking at and the values in order by time. That is used in the recursive portion below by joining to itself and evaluating each row compared to the next row (hence the RowRank - 1) on the JOIN.

提交回复
热议问题