title: SQL. Find the first and last value in a time interval, and then work with them
body: <p>Today I have started to optimize different procedures of the database in SQL Server.</p>
  <p>I would like to know if you can help me to optimize the following query:</p>
  <pre><code>DECLARE @fkTipoDato INTEGER = 8;
  DECLARE @fkPlanta INTEGER = 1;
  DECLARE @FechaInicio DATETIME = CAST('2020-27-10 00:00:00' AS DATETIME);
  DECLARE @FechaFin DATETIME = CAST('2020-28-10 00:00:00' AS DATETIME);
  DECLARE @FechaWhile DATETIME = @FechaInicio;
  DECLARE @valorMaximoMedidor REAL = 2999999;
  DECLARE @TablaTemp TABLE (
      tempFecha VARCHAR(20)
      ,cero REAL
      ,uno REAL
      ,dos REAL
      ,tres REAL
      ,cuatro REAL
      ,cinco REAL
      ,seis REAL
      ,siete REAL
      ,ocho REAL
      ,nueve REAL
      ,diez REAL
      ,ONCE REAL
      ,doce REAL
      ,trece REAL
      ,catorce REAL
      ,quince REAL
      ,dieciseis REAL
      ,diecisiete REAL
      ,dieciocho REAL
      ,diecinueve REAL
      ,veinte REAL
      ,veintiuno REAL
      ,veintidos REAL
      ,veintitres REAL
      );
  
  WHILE @FechaWhile &lt;= @FechaFin
  BEGIN
      SET NOCOUNT ON;
  
      DECLARE @ValorFinal0 REAL = (
              SELECT TOP 1 valor
              FROM RegistroDatos
              WHERE fk_TipoDato = @fkTipoDato
                  AND fk_Planta = @fkPlanta
                  AND FechaHora &gt;= CAST(@FechaWhile AS DATETIME)
                  AND FechaHora &lt;= DATEADD(MILLISECOND, - 1, DATEADD(HOUR, 1, @FechaWhile))
              ORDER BY FechaHora DESC
              );
      DECLARE @ValorInicial0 REAL = (
              SELECT TOP 1 valor
              FROM RegistroDatos
              WHERE fk_TipoDato = @fkTipoDato
                  AND fk_Planta = @fkPlanta
                  AND FechaHora &gt;= CAST(@FechaWhile AS DATETIME)
                  AND FechaHora &lt;= DATEADD(MILLISECOND, - 1, DATEADD(HOUR, 1, @FechaWhile))
              ORDER BY FechaHora ASC
              );
      DECLARE @ValorFinal1 REAL = (
              SELECT TOP 1 valor
              FROM RegistroDatos
              WHERE fk_TipoDato = @fkTipoDato
                  AND fk_Planta = @fkPlanta
                  AND FechaHora &gt;= DATEADD(HOUR, 1, @FechaWhile)
                  AND FechaHora &lt;= DATEADD(MILLISECOND, - 1, DATEADD(HOUR, 2, @FechaWhile))
              ORDER BY FechaHora DESC
              );
      DECLARE @ValorInicial1 REAL = (
              SELECT TOP 1 valor
              FROM RegistroDatos
              WHERE fk_TipoDato = @fkTipoDato
                  AND fk_Planta = @fkPlanta
                  AND FechaHora &gt;= DATEADD(HOUR, 1, @FechaWhile)
                  AND FechaHora &lt;= DATEADD(MILLISECOND, - 1, DATEADD(HOUR, 2, @FechaWhile))
              ORDER BY FechaHora ASC
              );
      DECLARE @ValorFinal2 REAL = (
              SELECT TOP 1 valor
              FROM RegistroDatos
              WHERE fk_TipoDato = @fkTipoDato
                  AND fk_Planta = @fkPlanta
                  AND FechaHora &gt;= DATEADD(HOUR, 2, @FechaWhile)
                  AND FechaHora &lt;= DATEADD(MILLISECOND, - 1, DATEADD(HOUR, 3, @FechaWhile))
              ORDER BY FechaHora DESC
              );
      DECLARE @ValorInicial2 REAL = (
              SELECT TOP 1 valor
              FROM RegistroDatos
              WHERE fk_TipoDato = @fkTipoDato
                  AND fk_Planta = @fkPlanta
                  AND FechaHora &gt;= DATEADD(HOUR, 2, @FechaWhile)
                  AND FechaHora &lt;= DATEADD(MILLISECOND, - 1, DATEADD(HOUR, 3, @FechaWhile))
              ORDER BY FechaHora ASC
              );
      DECLARE @ValorFinal3 REAL = (
              SELECT TOP 1 valor
              FROM RegistroDatos
              WHERE fk_TipoDato = @fkTipoDato
                  AND fk_Planta = @fkPlanta
                  AND FechaHora &gt;= DATEADD(HOUR, 3, @FechaWhile)
                  AND FechaHora &lt;= DATEADD(MILLISECOND, - 1, DATEADD(HOUR, 4, @FechaWhile))
              ORDER BY FechaHora DESC
              );
      DECLARE @ValorInicial3 REAL = (
              SELECT TOP 1 valor
              FROM RegistroDatos
              WHERE fk_TipoDato = @fkTipoDato
                  AND fk_Planta = @fkPlanta
                  AND FechaHora &gt;= DATEADD(HOUR, 3, @FechaWhile)
                  AND FechaHora &lt;= DATEADD(MILLISECOND, - 1, DATEADD(HOUR, 4, @FechaWhile))
              ORDER BY FechaHora ASC
              );
      DECLARE @ValorFinal4 REAL = (
              SELECT TOP 1 valor
              FROM RegistroDatos
              WHERE fk_TipoDato = @fkTipoDato
                  AND fk_Planta = @fkPlanta
                  AND FechaHora &gt;= DATEADD(HOUR, 4, @FechaWhile)
                  AND FechaHora &lt;= DATEADD(MILLISECOND, - 1, DATEADD(HOUR, 5, @FechaWhile))
              ORDER BY FechaHora DESC
              );
      DECLARE @ValorInicial4 REAL = (
              SELECT TOP 1 valor
              FROM RegistroDatos
              WHERE fk_TipoDato = @fkTipoDato
                  AND fk_Planta = @fkPlanta
                  AND FechaHora &gt;= DATEADD(HOUR, 4, @FechaWhile)
                  AND FechaHora &lt;= DATEADD(MILLISECOND, - 1, DATEADD(HOUR, 5, @FechaWhile))
              ORDER BY FechaHora ASC
              );
      DECLARE @ValorFinal5 REAL = (
              SELECT TOP 1 valor
              FROM RegistroDatos
              WHERE fk_TipoDato = @fkTipoDato
                  AND fk_Planta = @fkPlanta
                  AND FechaHora &gt;= DATEADD(HOUR, 5, @FechaWhile)
                  AND FechaHora &lt;= DATEADD(MILLISECOND, - 1, DATEADD(HOUR, 6, @FechaWhile))
              ORDER BY FechaHora DESC
              );
      DECLARE @ValorInicial5 REAL = (
              SELECT TOP 1 valor
              FROM RegistroDatos
              WHERE fk_TipoDato = @fkTipoDato
                  AND fk_Planta = @fkPlanta
                  AND FechaHora &gt;= DATEADD(HOUR, 5, @FechaWhile)
                  AND FechaHora &lt;= DATEADD(MILLISECOND, - 1, DATEADD(HOUR, 6, @FechaWhile))
              ORDER BY FechaHora ASC
              );
      DECLARE @ValorFinal6 REAL = (
              SELECT TOP 1 valor
              FROM RegistroDatos
              WHERE fk_TipoDato = @fkTipoDato
                  AND fk_Planta = @fkPlanta
                  AND FechaHora &gt;= DATEADD(HOUR, 6, @FechaWhile)
                  AND FechaHora &lt;= DATEADD(MILLISECOND, - 1, DATEADD(HOUR, 7, @FechaWhile))
              ORDER BY FechaHora DESC
              );
      DECLARE @ValorInicial6 REAL = (
              SELECT TOP 1 valor
              FROM RegistroDatos
              WHERE fk_TipoDato = @fkTipoDato
                  AND fk_Planta = @fkPlanta
                  AND FechaHora &gt;= DATEADD(HOUR, 6, @FechaWhile)
                  AND FechaHora &lt;= DATEADD(MILLISECOND, - 1, DATEADD(HOUR, 7, @FechaWhile))
              ORDER BY FechaHora ASC
              );
      DECLARE @ValorFinal7 REAL = (
              SELECT TOP 1 valor
              FROM RegistroDatos
              WHERE fk_TipoDato = @fkTipoDato
                  AND fk_Planta = @fkPlanta
                  AND FechaHora &gt;= DATEADD(HOUR, 7, @FechaWhile)
                  AND FechaHora &lt;= DATEADD(MILLISECOND, - 1, DATEADD(HOUR, 8, @FechaWhile))
              ORDER BY FechaHora DESC
              );
      DECLARE @ValorInicial7 REAL = (
              SELECT TOP 1 valor
              FROM RegistroDatos
              WHERE fk_TipoDato = @fkTipoDato
                  AND fk_Planta = @fkPlanta
                  AND FechaHora &gt;= DATEADD(HOUR, 7, @FechaWhile)
                  AND FechaHora &lt;= DATEADD(MILLISECOND, - 1, DATEADD(HOUR, 8, @FechaWhile))
              ORDER BY FechaHora ASC
              );
      DECLARE @ValorFinal8 REAL = (
              SELECT TOP 1 valor
              FROM RegistroDatos
              WHERE fk_TipoDato = @fkTipoDato
                  AND fk_Planta = @fkPlanta
                  AND FechaHora &gt;= DATEADD(HOUR, 8, @FechaWhile)
                  AND FechaHora &lt;= DATEADD(MILLISECOND, - 1, DATEADD(HOUR, 9, @FechaWhile))
              ORDER BY FechaHora DESC
              );
      DECLARE @ValorInicial8 REAL = (
              SELECT TOP 1 valor
              FROM RegistroDatos
              WHERE fk_TipoDato = @fkTipoDato
                  AND fk_Planta = @fkPlanta
                  AND FechaHora &gt;= DATEADD(HOUR, 8, @FechaWhile)
                  AND FechaHora &lt;= DATEADD(MILLISECOND, - 1, DATEADD(HOUR, 9, @FechaWhile))
              ORDER BY FechaHora ASC
              );
      DECLARE @ValorFinal9 REAL = (
              SELECT TOP 1 valor
              FROM RegistroDatos
              WHERE fk_TipoDato = @fkTipoDato
                  AND fk_Planta = @fkPlanta
                  AND FechaHora &gt;= DATEADD(HOUR, 9, @FechaWhile)
                  AND FechaHora &lt;= DATEADD(MILLISECOND, - 1, DATEADD(HOUR, 10, @FechaWhile))
              ORDER BY FechaHora DESC
              );
      DECLARE @ValorInicial9 REAL = (
              SELECT TOP 1 valor
              FROM RegistroDatos
              WHERE fk_TipoDato = @fkTipoDato
                  AND fk_Planta = @fkPlanta
                  AND FechaHora &gt;= DATEADD(HOUR, 9, @FechaWhile)
                  AND FechaHora &lt;= DATEADD(MILLISECOND, - 1, DATEADD(HOUR, 10, @FechaWhile))
              ORDER BY FechaHora ASC
              );
      DECLARE @ValorFinal10 REAL = (
              SELECT TOP 1 valor
              FROM RegistroDatos
              WHERE fk_TipoDato = @fkTipoDato
                  AND fk_Planta = @fkPlanta
                  AND FechaHora &gt;= DATEADD(HOUR, 10, @FechaWhile)
                  AND FechaHora &lt;= DATEADD(MILLISECOND, - 1, DATEADD(HOUR, 11, @FechaWhile))
              ORDER BY FechaHora DESC
              );
      DECLARE @ValorInicial10 REAL = (
              SELECT TOP 1 valor
              FROM RegistroDatos
              WHERE fk_TipoDato = @fkTipoDato
                  AND fk_Planta = @fkPlanta
                  AND FechaHora &gt;= DATEADD(HOUR, 10, @FechaWhile)
                  AND FechaHora &lt;= DATEADD(MILLISECOND, - 1, DATEADD(HOUR, 11, @FechaWhile))
              ORDER BY FechaHora ASC
              );
      DECLARE @ValorFinal11 REAL = (
              SELECT TOP 1 valor
              FROM RegistroDatos
              WHERE fk_TipoDato = @fkTipoDato
                  AND fk_Planta = @fkPlanta
                  AND FechaHora &gt;= DATEADD(HOUR, 11, @FechaWhile)
                  AND FechaHora &lt;= DATEADD(MILLISECOND, - 1, DATEADD(HOUR, 12, @FechaWhile))
              ORDER BY FechaHora DESC
              );
      DECLARE @ValorInicial11 REAL = (
              SELECT TOP 1 valor
              FROM RegistroDatos
              WHERE fk_TipoDato = @fkTipoDato
                  AND fk_Planta = @fkPlanta
                  AND FechaHora &gt;= DATEADD(HOUR, 11, @FechaWhile)
                  AND FechaHora &lt;= DATEADD(MILLISECOND, - 1, DATEADD(HOUR, 12, @FechaWhile))
              ORDER BY FechaHora ASC
              );
      DECLARE @ValorFinal12 REAL = (
              SELECT TOP 1 valor
              FROM RegistroDatos
              WHERE fk_TipoDato = @fkTipoDato
                  AND fk_Planta = @fkPlanta
                  AND FechaHora &gt;= DATEADD(HOUR, 12, @FechaWhile)
                  AND FechaHora &lt;= DATEADD(MILLISECOND, - 1, DATEADD(HOUR, 13, @FechaWhile))
              ORDER BY FechaHora DESC
              );
      DECLARE @ValorInicial12 REAL = (
              SELECT TOP 1 valor
              FROM RegistroDatos
              WHERE fk_TipoDato = @fkTipoDato
                  AND fk_Planta = @fkPlanta
                  AND FechaHora &gt;= DATEADD(HOUR, 12, @FechaWhile)
                  AND FechaHora &lt;= DATEADD(MILLISECOND, - 1, DATEADD(HOUR, 13, @FechaWhile))
              ORDER BY FechaHora ASC
              );
      DECLARE @ValorFinal13 REAL = (
              SELECT TOP 1 valor
              FROM RegistroDatos
              WHERE fk_TipoDato = @fkTipoDato
                  AND fk_Planta = @fkPlanta
                  AND FechaHora &gt;= DATEADD(HOUR, 13, @FechaWhile)
                  AND FechaHora &lt;= DATEADD(MILLISECOND, - 1, DATEADD(HOUR, 14, @FechaWhile))
              ORDER BY FechaHora DESC
              );
      DECLARE @ValorInicial13 REAL = (
              SELECT TOP 1 valor
              FROM RegistroDatos
              WHERE fk_TipoDato = @fkTipoDato
                  AND fk_Planta = @fkPlanta
                  AND FechaHora &gt;= DATEADD(HOUR, 13, @FechaWhile)
                  AND FechaHora &lt;= DATEADD(MILLISECOND, - 1, DATEADD(HOUR, 14, @FechaWhile))
              ORDER BY FechaHora ASC
              );
      DECLARE @ValorFinal14 REAL = (
              SELECT TOP 1 valor
              FROM RegistroDatos
              WHERE fk_TipoDato = @fkTipoDato
                  AND fk_Planta = @fkPlanta
                  AND FechaHora &gt;= DATEADD(HOUR, 14, @FechaWhile)
                  AND FechaHora &lt;= DATEADD(MILLISECOND, - 1, DATEADD(HOUR, 15, @FechaWhile))
              ORDER BY FechaHora DESC
              );
      DECLARE @ValorInicial14 REAL = (
              SELECT TOP 1 valor
              FROM RegistroDatos
              WHERE fk_TipoDato = @fkTipoDato
                  AND fk_Planta = @fkPlanta
                  AND FechaHora &gt;= DATEADD(HOUR, 14, @FechaWhile)
                  AND FechaHora &lt;= DATEADD(MILLISECOND, - 1, DATEADD(HOUR, 15, @FechaWhile))
              ORDER BY FechaHora ASC
              );
      DECLARE @ValorFinal15 REAL = (
              SELECT TOP 1 valor
              FROM RegistroDatos
              WHERE fk_TipoDato = @fkTipoDato
                  AND fk_Planta = @fkPlanta
                  AND FechaHora &gt;= DATEADD(HOUR, 15, @FechaWhile)
                  AND FechaHora &lt;= DATEADD(MILLISECOND, - 1, DATEADD(HOUR, 16, @FechaWhile))
              ORDER BY FechaHora DESC
              );
      DECLARE @ValorInicial15 REAL = (
              SELECT TOP 1 valor
              FROM RegistroDatos
              WHERE fk_TipoDato = @fkTipoDato
                  AND fk_Planta = @fkPlanta
                  AND FechaHora &gt;= DATEADD(HOUR, 15, @FechaWhile)
                  AND FechaHora &lt;= DATEADD(MILLISECOND, - 1, DATEADD(HOUR, 16, @FechaWhile))
              ORDER BY FechaHora ASC
              );
      DECLARE @ValorFinal16 REAL = (
              SELECT TOP 1 valor
              FROM RegistroDatos
              WHERE fk_TipoDato = @fkTipoDato
                  AND fk_Planta = @fkPlanta
                  AND FechaHora &gt;= DATEADD(HOUR, 16, @FechaWhile)
                  AND FechaHora &lt;= DATEADD(MILLISECOND, - 1, DATEADD(HOUR, 17, @FechaWhile))
              ORDER BY FechaHora DESC
              );
      DECLARE @ValorInicial16 REAL = (
              SELECT TOP 1 valor
              FROM RegistroDatos
              WHERE fk_TipoDato = @fkTipoDato
                  AND fk_Planta = @fkPlanta
                  AND FechaHora &gt;= DATEADD(HOUR, 16, @FechaWhile)
                  AND FechaHora &lt;= DATEADD(MILLISECOND, - 1, DATEADD(HOUR, 17, @FechaWhile))
              ORDER BY FechaHora ASC
              );
      DECLARE @ValorFinal17 REAL = (
              SELECT TOP 1 valor
              FROM RegistroDatos
              WHERE fk_TipoDato = @fkTipoDato
                  AND fk_Planta = @fkPlanta
                  AND FechaHora &gt;= DATEADD(HOUR, 17, @FechaWhile)
                  AND FechaHora &lt;= DATEADD(MILLISECOND, - 1, DATEADD(HOUR, 18, @FechaWhile))
              ORDER BY FechaHora DESC
              );
      DECLARE @ValorInicial17 REAL = (
              SELECT TOP 1 valor
              FROM RegistroDatos
              WHERE fk_TipoDato = @fkTipoDato
                  AND fk_Planta = @fkPlanta
                  AND FechaHora &gt;= DATEADD(HOUR, 17, @FechaWhile)
                  AND FechaHora &lt;= DATEADD(MILLISECOND, - 1, DATEADD(HOUR, 18, @FechaWhile))
              ORDER BY FechaHora ASC
              );
      DECLARE @ValorFinal18 REAL = (
              SELECT TOP 1 valor
              FROM RegistroDatos
              WHERE fk_TipoDato = @fkTipoDato
                  AND fk_Planta = @fkPlanta
                  AND FechaHora &gt;= DATEADD(HOUR, 18, @FechaWhile)
                  AND FechaHora &lt;= DATEADD(MILLISECOND, - 1, DATEADD(HOUR, 19, @FechaWhile))
              ORDER BY FechaHora DESC
              );
      DECLARE @ValorInicial18 REAL = (
              SELECT TOP 1 valor
              FROM RegistroDatos
              WHERE fk_TipoDato = @fkTipoDato
                  AND fk_Planta = @fkPlanta
                  AND FechaHora &gt;= DATEADD(HOUR, 18, @FechaWhile)
                  AND FechaHora &lt;= DATEADD(MILLISECOND, - 1, DATEADD(HOUR, 19, @FechaWhile))
              ORDER BY FechaHora ASC
              );
      DECLARE @ValorFinal19 REAL = (
              SELECT TOP 1 valor
              FROM RegistroDatos
              WHERE fk_TipoDato = @fkTipoDato
                  AND fk_Planta = @fkPlanta
                  AND FechaHora &gt;= DATEADD(HOUR, 19, @FechaWhile)
                  AND FechaHora &lt;= DATEADD(MILLISECOND, - 1, DATEADD(HOUR, 20, @FechaWhile))
              ORDER BY FechaHora DESC
              );
      DECLARE @ValorInicial19 REAL = (
              SELECT TOP 1 valor
              FROM RegistroDatos
              WHERE fk_TipoDato = @fkTipoDato
                  AND fk_Planta = @fkPlanta
                  AND FechaHora &gt;= DATEADD(HOUR, 19, @FechaWhile)
                  AND FechaHora &lt;= DATEADD(MILLISECOND, - 1, DATEADD(HOUR, 20, @FechaWhile))
              ORDER BY FechaHora ASC
              );
      DECLARE @ValorFinal20 REAL = (
              SELECT TOP 1 valor
              FROM RegistroDatos
              WHERE fk_TipoDato = @fkTipoDato
                  AND fk_Planta = @fkPlanta
                  AND FechaHora &gt;= DATEADD(HOUR, 20, @FechaWhile)
                  AND FechaHora &lt;= DATEADD(MILLISECOND, - 1, DATEADD(HOUR, 21, @FechaWhile))
              ORDER BY FechaHora DESC
              );
      DECLARE @ValorInicial20 REAL = (
              SELECT TOP 1 valor
              FROM RegistroDatos
              WHERE fk_TipoDato = @fkTipoDato
                  AND fk_Planta = @fkPlanta
                  AND FechaHora &gt;= DATEADD(HOUR, 20, @FechaWhile)
                  AND FechaHora &lt;= DATEADD(MILLISECOND, - 1, DATEADD(HOUR, 21, @FechaWhile))
              ORDER BY FechaHora ASC
              );
      DECLARE @ValorFinal21 REAL = (
              SELECT TOP 1 valor
              FROM RegistroDatos
              WHERE fk_TipoDato = @fkTipoDato
                  AND fk_Planta = @fkPlanta
                  AND FechaHora &gt;= DATEADD(HOUR, 21, @FechaWhile)
                  AND FechaHora &lt;= DATEADD(MILLISECOND, - 1, DATEADD(HOUR, 22, @FechaWhile))
              ORDER BY FechaHora DESC
              );
      DECLARE @ValorInicial21 REAL = (
              SELECT TOP 1 valor
              FROM RegistroDatos
              WHERE fk_TipoDato = @fkTipoDato
                  AND fk_Planta = @fkPlanta
                  AND FechaHora &gt;= DATEADD(HOUR, 21, @FechaWhile)
                  AND FechaHora &lt;= DATEADD(MILLISECOND, - 1, DATEADD(HOUR, 22, @FechaWhile))
              ORDER BY FechaHora ASC
              );
      DECLARE @ValorFinal22 REAL = (
              SELECT TOP 1 valor
              FROM RegistroDatos
              WHERE fk_TipoDato = @fkTipoDato
                  AND fk_Planta = @fkPlanta
                  AND FechaHora &gt;= DATEADD(HOUR, 22, @FechaWhile)
                  AND FechaHora &lt;= DATEADD(MILLISECOND, - 1, DATEADD(HOUR, 23, @FechaWhile))
              ORDER BY FechaHora DESC
              );
      DECLARE @ValorInicial22 REAL = (
              SELECT TOP 1 valor
              FROM RegistroDatos
              WHERE fk_TipoDato = @fkTipoDato
                  AND fk_Planta = @fkPlanta
                  AND FechaHora &gt;= DATEADD(HOUR, 22, @FechaWhile)
                  AND FechaHora &lt;= DATEADD(MILLISECOND, - 1, DATEADD(HOUR, 23, @FechaWhile))
              ORDER BY FechaHora ASC
              );
      DECLARE @ValorFinal23 REAL = (
              SELECT TOP 1 valor
              FROM RegistroDatos
              WHERE fk_TipoDato = @fkTipoDato
                  AND fk_Planta = @fkPlanta
                  AND FechaHora &gt;= DATEADD(HOUR, 23, @FechaWhile)
                  AND FechaHora &lt;= DATEADD(MILLISECOND, - 1, DATEADD(HOUR, 24, @FechaWhile))
              ORDER BY FechaHora DESC
              );
      DECLARE @ValorInicial23 REAL = (
              SELECT TOP 1 valor
              FROM RegistroDatos
              WHERE fk_TipoDato = @fkTipoDato
                  AND fk_Planta = @fkPlanta
                  AND FechaHora &gt;= DATEADD(HOUR, 23, @FechaWhile)
                  AND FechaHora &lt;= DATEADD(MILLISECOND, - 1, DATEADD(HOUR, 24, @FechaWhile))
              ORDER BY FechaHora ASC
              );
  
      IF EXISTS (
              SELECT TOP 1 FechaHora
              FROM RegistroDatos
              WHERE fk_TipoDato = @fkTipoDato
                  AND FechaHora &gt;= @FechaWhile
                  AND FechaHora &lt;= DATEADD(day, 1, @FechaWhile)
                  AND fk_Planta = @fkPlanta
              )
      BEGIN
          INSERT INTO @TablaTemp
          SELECT TOP 1 CONVERT(VARCHAR(10), FechaHora, 103) AS [Fecha]
              ,(
                  CASE 
                      WHEN @ValorFinal0 &gt;= @ValorInicial0
                          THEN (
                                  SELECT (@ValorFinal0 - @ValorInicial0)
                                  )
                      ELSE (
                              SELECT ((@valorMaximoMedidor - @ValorInicial0) + @ValorFinal0)
                              )
                      END
                  ) AS [00:00]
              ,(
                  CASE 
                      WHEN @ValorFinal1 &gt;= @ValorInicial1
                          THEN (
                                  SELECT (@ValorFinal1 - @ValorInicial1)
                                  )
                      ELSE (
                              SELECT ((@valorMaximoMedidor - @ValorInicial1) + @ValorFinal1)
                              )
                      END
                  ) AS [01:00]
              ,(
                  CASE 
                      WHEN @ValorFinal2 &gt;= @ValorInicial2
                          THEN (
                                  SELECT (@ValorFinal2 - @ValorInicial2)
                                  )
                      ELSE (
                              SELECT ((@valorMaximoMedidor - @ValorInicial2) + @ValorFinal2)
                              )
                      END
                  ) AS [02:00]
              ,(
                  CASE 
                      WHEN @ValorFinal3 &gt;= @ValorInicial3
                          THEN (
                                  SELECT (@ValorFinal3 - @ValorInicial3)
                                  )
                      ELSE (
                              SELECT ((@valorMaximoMedidor - @ValorInicial3) + @ValorFinal3)
                              )
                      END
                  ) AS [03:00]
              ,(
                  CASE 
                      WHEN @ValorFinal4 &gt;= @ValorInicial4
                          THEN (
                                  SELECT (@ValorFinal4 - @ValorInicial4)
                                  )
                      ELSE (
                              SELECT ((@valorMaximoMedidor - @ValorInicial4) + @ValorFinal4)
                              )
                      END
                  ) AS [04:00]
              ,(
                  CASE 
                      WHEN @ValorFinal5 &gt;= @ValorInicial5
                          THEN (
                                  SELECT (@ValorFinal5 - @ValorInicial5)
                                  )
                      ELSE (
                              SELECT ((@valorMaximoMedidor - @ValorInicial5) + @ValorFinal5)
                              )
                      END
                  ) AS [05:00]
              ,(
                  CASE 
                      WHEN @ValorFinal6 &gt;= @ValorInicial6
                          THEN (
                                  SELECT (@ValorFinal6 - @ValorInicial6)
                                  )
                      ELSE (
                              SELECT ((@valorMaximoMedidor - @ValorInicial6) + @ValorFinal6)
                              )
                      END
                  ) AS [06:00]
              ,(
                  CASE 
                      WHEN @ValorFinal7 &gt;= @ValorInicial7
                          THEN (
                                  SELECT (@ValorFinal7 - @ValorInicial7)
                                  )
                      ELSE (
                              SELECT ((@valorMaximoMedidor - @ValorInicial7) + @ValorFinal7)
                              )
                      END
                  ) AS [07:00]
              ,(
                  CASE 
                      WHEN @ValorFinal8 &gt;= @ValorInicial8
                          THEN (
                                  SELECT (@ValorFinal8 - @ValorInicial8)
                                  )
                      ELSE (
                              SELECT ((@valorMaximoMedidor - @ValorInicial8) + @ValorFinal8)
                              )
                      END
                  ) AS [08:00]
              ,(
                  CASE 
                      WHEN @ValorFinal9 &gt;= @ValorInicial9
                          THEN (
                                  SELECT (@ValorFinal9 - @ValorInicial9)
                                  )
                      ELSE (
                              SELECT ((@valorMaximoMedidor - @ValorInicial9) + @ValorFinal9)
                              )
                      END
                  ) AS [09:00]
              ,(
                  CASE 
                      WHEN @ValorFinal10 &gt;= @ValorInicial10
                          THEN (
                                  SELECT (@ValorFinal10 - @ValorInicial10)
                                  )
                      ELSE (
                              SELECT ((@valorMaximoMedidor - @ValorInicial10) + @ValorFinal10)
                              )
                      END
                  ) AS [10:00]
              ,(
                  CASE 
                      WHEN @ValorFinal11 &gt;= @ValorInicial11
                          THEN (
                                  SELECT (@ValorFinal11 - @ValorInicial11)
                                  )
                      ELSE (
                              SELECT ((@valorMaximoMedidor - @ValorInicial11) + @ValorFinal11)
                              )
                      END
                  ) AS [11:00]
              ,(
                  CASE 
                      WHEN @ValorFinal12 &gt;= @ValorInicial12
                          THEN (
                                  SELECT (@ValorFinal12 - @ValorInicial12)
                                  )
                      ELSE (
                              SELECT ((@valorMaximoMedidor - @ValorInicial12) + @ValorFinal12)
                              )
                      END
                  ) AS [12:00]
              ,(
                  CASE 
                      WHEN @ValorFinal13 &gt;= @ValorInicial13
                          THEN (
                                  SELECT (@ValorFinal13 - @ValorInicial13)
                                  )
                      ELSE (
                              SELECT ((@valorMaximoMedidor - @ValorInicial13) + @ValorFinal13)
                              )
                      END
                  ) AS [13:00]
              ,(
                  CASE 
                      WHEN @ValorFinal14 &gt;= @ValorInicial14
                          THEN (
                                  SELECT (@ValorFinal14 - @ValorInicial14)
                                  )
                      ELSE (
                              SELECT ((@valorMaximoMedidor - @ValorInicial14) + @ValorFinal14)
                              )
                      END
                  ) AS [14:00]
              ,(
                  CASE 
                      WHEN @ValorFinal15 &gt;= @ValorInicial15
                          THEN (
                                  SELECT (@ValorFinal15 - @ValorInicial15)
                                  )
                      ELSE (
                              SELECT ((@valorMaximoMedidor - @ValorInicial15) + @ValorFinal15)
                              )
                      END
                  ) AS [15:00]
              ,(
                  CASE 
                      WHEN @ValorFinal16 &gt;= @ValorInicial16
                          THEN (
                                  SELECT (@ValorFinal16 - @ValorInicial16)
                                  )
                      ELSE (
                              SELECT ((@valorMaximoMedidor - @ValorInicial16) + @ValorFinal16)
                              )
                      END
                  ) AS [16:00]
              ,(
                  CASE 
                      WHEN @ValorFinal17 &gt;= @ValorInicial17
                          THEN (
                                  SELECT (@ValorFinal17 - @ValorInicial17)
                                  )
                      ELSE (
                              SELECT ((@valorMaximoMedidor - @ValorInicial17) + @ValorFinal17)
                              )
                      END
                  ) AS [17:00]
              ,(
                  CASE 
                      WHEN @ValorFinal18 &gt;= @ValorInicial18
                          THEN (
                                  SELECT (@ValorFinal18 - @ValorInicial18)
                                  )
                      ELSE (
                              SELECT ((@valorMaximoMedidor - @ValorInicial18) + @ValorFinal18)
                              )
                      END
                  ) AS [18:00]
              ,(
                  CASE 
                      WHEN @ValorFinal19 &gt;= @ValorInicial19
                          THEN (
                                  SELECT (@ValorFinal19 - @ValorInicial19)
                                  )
                      ELSE (
                              SELECT ((@valorMaximoMedidor - @ValorInicial19) + @ValorFinal19)
                              )
                      END
                  ) AS [19:00]
              ,(
                  CASE 
                      WHEN @ValorFinal20 &gt;= @ValorInicial20
                          THEN (
                                  SELECT (@ValorFinal20 - @ValorInicial20)
                                  )
                      ELSE (
                              SELECT ((@valorMaximoMedidor - @ValorInicial20) + @ValorFinal20)
                              )
                      END
                  ) AS [20:00]
              ,(
                  CASE 
                      WHEN @ValorFinal21 &gt;= @ValorInicial21
                          THEN (
                                  SELECT (@ValorFinal21 - @ValorInicial21)
                                  )
                      ELSE (
                              SELECT ((@valorMaximoMedidor - @ValorInicial21) + @ValorFinal21)
                              )
                      END
                  ) AS [21:00]
              ,(
                  CASE 
                      WHEN @ValorFinal22 &gt;= @ValorInicial22
                          THEN (
                                  SELECT (@ValorFinal22 - @ValorInicial22)
                                  )
                      ELSE (
                              SELECT ((@valorMaximoMedidor - @ValorInicial22) + @ValorFinal22)
                              )
                      END
                  ) AS [22:00]
              ,(
                  CASE 
                      WHEN @ValorFinal23 &gt;= @ValorInicial23
                          THEN (
                                  SELECT (@ValorFinal23 - @ValorInicial23)
                                  )
                      ELSE (
                              SELECT ((@valorMaximoMedidor - @ValorInicial23) + @ValorFinal23)
                              )
                      END
                  ) AS [23:00]
          FROM RegistroDatos
          WHERE fk_TipoDato = @fkTipoDato
              AND FechaHora &gt;= @FechaWhile
              AND FechaHora &lt;= DATEADD(day, 1, @FechaWhile)
              AND fk_Planta = @fkPlanta
      END
  
      SET @FechaWhile = DATEADD(day, 1, @FechaWhile);
  END
  
  SELECT tempFecha AS [Fecha]
      ,cero AS [00:00]
      ,uno AS [01:00]
      ,dos AS [02:00]
      ,tres AS [03:00]
      ,cuatro AS [04:00]
      ,cinco AS [05:00]
      ,seis AS [06:00]
      ,siete AS [07:00]
      ,ocho AS [08:00]
      ,nueve AS [09:00]
      ,diez AS [10:00]
      ,ONCE AS [11:00]
      ,doce AS [12:00]
      ,trece AS [13:00]
      ,catorce AS [14:00]
      ,quince AS [15:00]
      ,dieciseis AS [16:00]
      ,diecisiete AS [17:00]
      ,dieciocho AS [18:00]
      ,diecinueve AS [19:00]
      ,veinte AS [20:00]
      ,veintiuno AS [21:00]
      ,veintidos AS [22:00]
      ,veintitres AS [23:00]
  FROM @TablaTemp
  </code></pre>
  <p>It returns the consumptions (difference between maximum and minimum value) separated by hours and days (which would be easy to do), but the problem appears since when data is saved and the value = 2999999 is reached, the counter is reset. So you should check the first and last values instead of the minimum and maximum, and then work with them.</p>
  <p>As well as attach the code, it works, but it is slow.</p>
  <p>VERSION: Microsoft SQL Server 2008 R2 (RTM)</p>
  <p>The RegistroDatos table has the following layout:
  <a href="https://i.stack.imgur.com/P3EvS.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/P3EvS.png" alt="DDL" /></a>
  <a href="https://i.stack.imgur.com/zLxDZ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/zLxDZ.png" alt="RegistroDatos Design" /></a></p>
  <p><a href="https://i.stack.imgur.com/zj9ma.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/zj9ma.png" alt="RegistroDatos" /></a></p>
  <p>Any suggestion?</p>