SSMS
 sql >> Database >  >> Database Tools >> SSMS

Query SQL Confronta i valori ogni 15 minuti e visualizza il risultato ogni ora

Di seguito è riportata la query di cui hai bisogno e una soluzione funzionante Nota:ho modificato l'intervallo di tempo in 24 ore

       ;with SourceData(HourTime, Value, RowNum)
  as
  (
    select 
      datepart(hh, UTCTime) HourTime, 
      Value, 
      row_number() over (partition by datepart(hh, UTCTime) order by UTCTime) RowNum
    from foo
    union 
    select 
        datepart(hh, UTCTime) - 1 HourTime, 
        Value,
        5
    from foo
    where datepart(mi, UTCTime) = 0
  )
  select cast(A.HourTime as varchar) + ':00' UTCTime, sum(case when A.Value = B.Value then 1 else 0 end) ConstantValues
  from SourceData A
   inner join SourceData B on A.HourTime = B.HourTime and
                           (B.RowNum = (A.RowNum - 1))
  group by cast(A.HourTime as varchar) + ':00'