查找间隔 15 分钟的数据集中的最后一个值

To find the last value in the dataset of 15 minutes interval

ID Timestamp Value                                                               
   1   11:59.54   10

   1   12.04.00   20

   1   12.12.00   31

   1   12.16.00   10

   1   12.48.00   05

我想要结果集为

ID Timestamp Value

 1   11:59.54   10

   1   12:00:00   10

   1   12.04.00   20

   1   12.12.00   31

   1   12:15:00   31

   1   12:16.00   10

   1   12:30:00   10

   1   12:45:00   10

   1   12.48.00   05

更多的咖啡可能会导致更简单的解决方案,但请考虑以下...

DROP TABLE IF EXISTS my_table;

CREATE TABLE my_table
(id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
,timestamp TIMESTAMP
,value INT NOT NULL
);

INSERT INTO my_table VALUES
(1   ,'11:59:54',10),
(2   ,'12:04:00',20),
(3   ,'12:12:00',31),
(4   ,'12:16:00',10),
(5   ,'12:48:00',05);

...此外,我有一个 table 整数,看起来像这样:

SELECT * FROM ints;
+---+
| i |
+---+
| 0 |
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 6 |
| 7 |
| 8 |
| 9 |
+---+

所以...

SELECT a.timestamp
     , b.value
  FROM 
     ( SELECT x.*
            , MIN(y.timestamp) min_timestamp
         FROM 
            ( SELECT timestamp
                FROM my_table
               UNION
              SELECT SEC_TO_TIME((i2.i*10+i1.i)*900)
                FROM ints i1
                   , ints i2 
               WHERE SEC_TO_TIME((i2.i*10+i1.i)*900) 
                     BETWEEN (SELECT MIN(timestamp) FROM my_table) 
                         AND (SELECT MAX(timestamp) FROM my_table) 
               ORDER 
                  BY timestamp
            ) x
         LEFT
         JOIN my_table y
           ON y.timestamp >= x.timestamp
        GROUP 
           BY x.timestamp
    ) a
 JOIN my_table b
   ON b.timestamp = min_timestamp;

+-----------+-------+
| timestamp | value |
+-----------+-------+
| 11:59:54  |    10 |
| 12:00:00  |    20 |
| 12:04:00  |    20 |
| 12:12:00  |    31 |
| 12:15:00  |    10 |
| 12:16:00  |    10 |
| 12:30:00  |     5 |
| 12:45:00  |     5 |
| 12:48:00  |     5 |
+-----------+-------+

思路如下。使用 SERIES_GENERATE() to generate the missing time stamps with the 15 minute intervals and and union it with the existing data your table T. Now you would want to use LAST_VALUE with IGNORE NULLS. IGNORE NULLS is not implemented in HANA, therefore you have to do a bit of a workaround. I use COUNT() as a window function 计算非空值。我对原始数据执行相同的操作,然后将两者加入计数。这样我就重复了最后一个非空值。

select X.ID, X.TIME, Y.VALUE from (
    select ID, TIME, value, 
        count(VALUE) over (order by TIME rows between unbounded preceding and current row) as CNT 
    from (
        --add the missing 15 minute interval timestamps
        select 1 as ID, GENERATED_PERIOD_START as TIME, NULL as VALUE 
        from SERIES_GENERATE_TIME('INTERVAL 15 MINUTE', '12:00:00', '13:00:00')
        union all 
        select ID, TIME, VALUE from T
    )
) as X join (
    select ID, TIME, value, 
           count(value) over (order by TIME rows between unbounded preceding and current row) as CNT
    from T
) as Y on X.CNT = Y.CNT