Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 如何检查表示多小时时间序列(DB2)的表中缺少的值?_Sql_Database_Timestamp_Db2 - Fatal编程技术网

Sql 如何检查表示多小时时间序列(DB2)的表中缺少的值?

Sql 如何检查表示多小时时间序列(DB2)的表中缺少的值?,sql,database,timestamp,db2,Sql,Database,Timestamp,Db2,假设我有一个名为FOO的DB2表,它有一个时间序列id、一个每小时的时间戳值和一个整数。这就是定义: CREATE TABLE FOO( Id_timeseries INTEGER NOT NULL, number DECIMAL(10, 3) NOT NULL, timestamp TIMESTAMP NOT NULL, ); 我想知道,对于每个时间序列(假设有几个时间序列),在两个给定的日期之间是否有空值,这些空

假设我有一个名为FOO的DB2表,它有一个时间序列id、一个每小时的时间戳值和一个整数。这就是定义:

CREATE TABLE FOO(
    Id_timeseries      INTEGER NOT NULL,
    number              DECIMAL(10, 3) NOT NULL,
    timestamp          TIMESTAMP NOT NULL,
);
我想知道,对于每个时间序列(假设有几个时间序列),在两个给定的日期之间是否有空值,这些空值是什么(我想给出这些值的范围会困难得多)

例如:

Id_timeseries    number     timestamp
1                 28        2017-01-01 01:00:00
1                 28        2017-01-01 02:00:00
1                 28        2017-01-01 03:00:00
2                 28        2017-01-01 01:00:00
2                 28        2017-01-01 02:00:00
2                 28        2017-01-01 03:00:00
1                 28        2017-01-01 07:00:00
1                 28        2017-01-01 06:00:00
我想知道从
2017:01-01 00:00:00
2017:01-02 00:00:00

输出:

Id_timeseries    from                   to
1                2017:01-01 00:00:00    2017:01-01 00:00:00
1                2017:01-01 04:00:00    2017:01-01 05:00:00
1                2017:01-01 08:00:00    2017:01-01 23:00:00
2                2017:01-01 00:00:00    2017:01-01 00:00:00
2                2017:01-01 04:00:00    2017:01-01 23:00:00
试试这个:

WITH FOO (Id_timeseries, number, timestamp) AS 
(
VALUES
  (1, 28, timestamp('2017-01-01 01:00:00'))
, (1, 28, timestamp('2017-01-01 02:00:00'))
, (1, 28, timestamp('2017-01-01 03:00:00'))
, (1, 28, timestamp('2017-01-01 06:00:00'))
, (1, 28, timestamp('2017-01-01 07:00:00'))
--, (1, 28, timestamp('2017-01-01 00:00:00'))
--, (1, 28, timestamp('2017-01-01 23:00:00'))
--, (1, 28, timestamp('2017-01-02 00:00:00'))
, (2, 28, timestamp('2017-01-01 01:00:00'))
, (2, 28, timestamp('2017-01-01 02:00:00'))
, (2, 28, timestamp('2017-01-01 03:00:00'))
)
-- Internal gaps
SELECT Id_timeseries, timestamp_prev + 1 hour as from, timestamp - 1 hour as to
FROM
(
SELECT Id_timeseries, timestamp, lag(timestamp) over (partition by Id_timeseries order by timestamp) timestamp_prev
FROM FOO
)
WHERE timestamp_prev <> timestamp - 1 hour
-- Start gap
  UNION ALL
SELECT Id_timeseries, timestamp('2017-01-01 00:00:00') as from, min(timestamp) - 1 hour as to
FROM FOO
GROUP BY Id_timeseries
HAVING timestamp('2017-01-01 00:00:00') <> min(timestamp)
-- End gap
  UNION ALL
SELECT Id_timeseries, max(timestamp) + 1 hour as from, timestamp('2017-01-02 00:00:00') as to 
FROM FOO
GROUP BY Id_timeseries
HAVING timestamp('2017-01-02 00:00:00') <> max(timestamp)
ORDER BY Id_timeseries, from;

请提供示例数据和所需结果。@GordonLinoff感谢您的建议。给你。
|ID_TIMESERIES|FROM                      |TO                        |
|-------------|--------------------------|--------------------------|
|1            |2017-01-01-00.00.00.000000|2017-01-01-00.00.00.000000|
|1            |2017-01-01-04.00.00.000000|2017-01-01-05.00.00.000000|
|1            |2017-01-01-08.00.00.000000|2017-01-02-00.00.00.000000|
|2            |2017-01-01-00.00.00.000000|2017-01-01-00.00.00.000000|
|2            |2017-01-01-04.00.00.000000|2017-01-02-00.00.00.000000|