SQL Server查询以获得连续出现的开始和结束

时间:2016-10-06 13:05:47

标签: tsql

我有一个包含3列(机器,时间,状态)的表,它存储有关不同系统及其在不同时间捕获的状态的信息。

Machine Time                 status
-----------------------------------
MAC_1   2016-10-06 06:48    OFF
MAC_1   2016-10-06 07:48    OFF
MAC_1   2016-10-06 08:48    ON
MAC_1   2016-10-06 09:48    ON
MAC_1   2016-10-06 10:48    ON
MAC_1   2016-10-06 11:48    OFF
MAC_1   2016-10-06 12:48    OFF
MAC_2   2016-10-06 06:48    OFF
MAC_2   2016-10-06 07:48    OFF
MAC_2   2016-10-06 08:48    OFF
MAC_2   2016-10-06 09:48    ON
MAC_2   2016-10-06 10:48    ON
MAC_2   2016-10-06 11:48    OFF

现在我正试图连续#34;每台机器的关闭和开启时间窗口。

我们试图实现的结果集如下,

Machine   Status   StartTime               EndTime 
-----------------------------------------------------------
MAC_1     OFF      2016-10-06 06:48        2016-10-06 07:48
MAC_1     ON       2016-10-06 08:48        2016-10-06 10:48
MAC_1     OFF      2016-10-06 11:48        2016-10-06 12:48
MAC_2     OFF      2016-10-06 06:48        2016-10-06 08:48
MAC_2     ON       2016-10-06 09:48        2016-10-06 10:48
MAC_2     OFF      2016-10-06 11:48        2016-10-06 11:48

请帮忙。

此致 RON

2 个答案:

答案 0 :(得分:2)

这是一个典型的数据岛问题。如果您有SQL Server 2012或更高版本,那么您可以使用窗口函数来获得所需的结果,如下例所示:

function setBackgroundSize() {
    var $sliderItem = $(".full-photos li");
    $sliderItem.each(function(){
      var imageSrc = $(this).data('image-src');
      imageSrc.replace(/url\((['"])?(.*?)\1\)/gi, '$2')
      .split(',')[0];
      var image = new Image();
      image.src = imageSrc;
      var width = image.width,
      height = image.height,
      ratio = width/height;
      console.log(ratio);
      if (ratio < 1) {
        $(this).css('background-size', 'contain');
      }
    });
}
$(window).load(setBackgroundSize());

答案 1 :(得分:1)

如果你有sql server 2008。

create table #test(mac varchar(100), mac_dt datetime, mac_stat varchar(100))

insert into #test values
('MAC_1', '2016-10-06 06:48', 'OFF') 
,('MAC_1', '2016-10-06 07:48', 'OFF')
,('MAC_1', '2016-10-06 08:48', 'ON')
,('MAC_1', '2016-10-06 09:48', 'ON')
,('MAC_1', '2016-10-06 10:48', 'ON')
,('MAC_1', '2016-10-06 11:48', 'OFF')
,('MAC_1', '2016-10-06 12:48', 'OFF')
,('MAC_2', '2016-10-06 06:48', 'OFF')
,('MAC_2', '2016-10-06 07:48', 'OFF')
,('MAC_2', '2016-10-06 08:48', 'OFF')
,('MAC_2', '2016-10-06 09:48', 'ON')
,('MAC_2', '2016-10-06 10:48', 'ON')
,('MAC_2', '2016-10-06 11:48', 'OFF');

with cte as
(select mac, mac_dt, mac_stat, row_number() over(order by mac, mac_dt) - row_number() over(order by mac, mac_stat) as grp
 from #test)
select t1.mac Machine, t3.mac_stat [Status], t3.StartTime, t3.EndTime
from #test t1 cross apply(select t2.mac, min(mac_dt) as StartTime, max(mac_dt) as EndTime, t2.mac_stat
                            from cte t2 
                            where t1.mac = t2.mac and t1.mac_stat = t2.mac_stat
                            group by t2.mac, t2.grp, t2.mac_stat) t3
where t1.mac = t3.mac and t1.mac_stat = t3.mac_stat
and t1.mac_dt = t3.StartTime                                            
order by t1.mac, t3.StartTime