Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.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 一个月内每小时的数据缺口_Sql_Sql Server_Gaps And Islands - Fatal编程技术网

Sql 一个月内每小时的数据缺口

Sql 一个月内每小时的数据缺口,sql,sql-server,gaps-and-islands,Sql,Sql Server,Gaps And Islands,嗨,各位飞越者 在我看来,我真的很难找到正确的方法来解决这个相对简单的差距查找问题。 我有一个表,其中每小时datetimes每小时日志文件导入到DB。 我需要在一段时间内找到丢失的时间,比如说四月。 因此,想象一下在DB表[imported_logs]中有以下数据 [2018-04-02 10:00:000] [2018-04-02 11:00:000] [2018-04-02 12:00:000] [2018-04-02 17:00:000] 我需要4月份差距分析的结果为: [

嗨,各位飞越者

在我看来,我真的很难找到正确的方法来解决这个相对简单的差距查找问题。 我有一个表,其中每小时datetimes每小时日志文件导入到DB。 我需要在一段时间内找到丢失的时间,比如说四月。 因此,想象一下在DB表[imported_logs]中有以下数据

[2018-04-02 10:00:000]
[2018-04-02 11:00:000]
[2018-04-02 12:00:000] 
[2018-04-02 17:00:000]
我需要4月份差距分析的结果为:

[      GAP-BEGIN     ]  [     GAB_END        ]
[2018-04-01 00:00:000]  [2018-04-02 10:00:000] <-- problem
[2018-04-02 13:00:000]  [2018-04-02 17:00:000] <-- can be found using below code
[2018-04-02 18:00:000]  [2018-05-01 00:00:000] <-- problem
因此,基本上,如果4月份期间没有导入日志,那么当前的实现将不会显示任何缺失的数据,这有点错误

我在想,我需要在4月初和4月底之前添加一些新的数据点,以使查询能够捕捉到月初和月底的缺失数据? 你们聪明的男孩/女孩会做什么


/对于这种情况,只需添加初始值和结束值(如果合适):

<your query here>
union all
select '2018-04-01 00:00:000', min(lit.hourImported)
from logsImportedTable lit
where lit.hourImported >= '2018-04-01 00:00:00'
having min(lit.hourImported) > '2018-04-01 00:00:00'
union all
select '2018-05-01 00:00:000', max(lit.hourImported)
from logsImportedTable lit
where lit.hourImported <= '2018-05-01 00:00:00'
having max(lit.hourImported) > '2018-05-01 00:00:00';

好的,谢谢@Gordon的大力帮助,这是我对这个问题的最终解决方案。即使整个月的数据缺失,并且所有的小缺口都在其中,它也会给出缺口

DECLARE @zone INT = 1, @currentPeriodStart DATETIME = '2018-01-01', 
@currentPeriodEnd DATETIME = '2018-02-01';

WITH t AS (
SELECT  *, rn = ROW_NUMBER() over (PARTITION BY zone_id ORDER BY 
time_of_file_present)
FROM  test
Where time_of_file_present > @currentPeriodStart and time_of_file_present < 
@currentPeriodEnd and zone_id = @zone
)  
SELECT  t1.zone_id, t1.time_of_file_present as gap_start, 
t2.time_of_file_present as gap_end
FROM    t t1
    INNER JOIN t t2 ON t2.zone_id = t1.zone_id AND t2.rn = t1.rn + 1
WHERE   DATEDIFF(MINUTE, t1.time_of_file_present, t2.time_of_file_present) >60 

union all
select @zone, @currentPeriodStart, min(lit.time_of_file_present)
from test lit
where lit.time_of_file_present >=  @currentPeriodStart
having min(lit.time_of_file_present) >  @currentPeriodStart and 
min(lit.time_of_file_present) < @currentPeriodEnd

union all
select @zone,max(lit.time_of_file_present), @currentPeriodEnd
from test lit
where lit.time_of_file_present <= @currentPeriodEnd
having max(lit.time_of_file_present) < @currentPeriodEnd and 
max(lit.time_of_file_present) > @currentPeriodStart

union all
select @zone,@currentPeriodStart, @currentPeriodEnd
from test lit
having max(lit.time_of_file_present) < @currentPeriodStart or 
max(lit.time_of_file_present) > @currentPeriodEnd
order by gap_start

搜索:SQL缺口Island我已经搜索了很多,因此能够找到数据内部的缺口。但不是4月底不存在的数据缺口。。。这是我的问题,我还没有找到一个合适的解决方案…Thx的这-它的工作几乎完美!但对于没有可用数据的月份,应以当前形式显示2018-01-01 00:00:00至2018-02-01 00:00:000的差距,它将显示整体可用数据的最小值或最大值,如7月数据:区域id差距开始差距结束1 2018-05-31 11:00:00.000 2018-08-01 00:00:00。000@KristianT . . . 只需在整个月内添加另一个工会。
<your query here>
union all
select '2018-04-01 00:00:000', min(lit.hourImported)
from logsImportedTable lit
where lit.hourImported >= '2018-04-01 00:00:00'
having min(lit.hourImported) > '2018-04-01 00:00:00'
union all
select '2018-05-01 00:00:000', max(lit.hourImported)
from logsImportedTable lit
where lit.hourImported <= '2018-05-01 00:00:00'
having max(lit.hourImported) > '2018-05-01 00:00:00';
DECLARE @zone INT = 1, @currentPeriodStart DATETIME = '2018-01-01', 
@currentPeriodEnd DATETIME = '2018-02-01';

WITH t AS (
SELECT  *, rn = ROW_NUMBER() over (PARTITION BY zone_id ORDER BY 
time_of_file_present)
FROM  test
Where time_of_file_present > @currentPeriodStart and time_of_file_present < 
@currentPeriodEnd and zone_id = @zone
)  
SELECT  t1.zone_id, t1.time_of_file_present as gap_start, 
t2.time_of_file_present as gap_end
FROM    t t1
    INNER JOIN t t2 ON t2.zone_id = t1.zone_id AND t2.rn = t1.rn + 1
WHERE   DATEDIFF(MINUTE, t1.time_of_file_present, t2.time_of_file_present) >60 

union all
select @zone, @currentPeriodStart, min(lit.time_of_file_present)
from test lit
where lit.time_of_file_present >=  @currentPeriodStart
having min(lit.time_of_file_present) >  @currentPeriodStart and 
min(lit.time_of_file_present) < @currentPeriodEnd

union all
select @zone,max(lit.time_of_file_present), @currentPeriodEnd
from test lit
where lit.time_of_file_present <= @currentPeriodEnd
having max(lit.time_of_file_present) < @currentPeriodEnd and 
max(lit.time_of_file_present) > @currentPeriodStart

union all
select @zone,@currentPeriodStart, @currentPeriodEnd
from test lit
having max(lit.time_of_file_present) < @currentPeriodStart or 
max(lit.time_of_file_present) > @currentPeriodEnd
order by gap_start