Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/25.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 server 查找两个特定SQL行之间的时差并填充新表_Sql Server - Fatal编程技术网

Sql server 查找两个特定SQL行之间的时差并填充新表

Sql server 查找两个特定SQL行之间的时差并填充新表,sql-server,Sql Server,我有一个表格,下面是一个缩略版本。我需要找到每个ID脱机的时间。但是,如果一个ID没有联机,它应该被忽略,每个ID可以脱机多次,但需要先联机才能脱机 |ID | Description | Time | |---|-------------|--------------------| |1 |Offline |'2017-09-07 12:53:02| |---|-------------|--------------------| |2 |Offlin

我有一个表格,下面是一个缩略版本。我需要找到每个ID脱机的时间。但是,如果一个ID没有联机,它应该被忽略,每个ID可以脱机多次,但需要先联机才能脱机

|ID | Description | Time               |
|---|-------------|--------------------|
|1  |Offline      |'2017-09-07 12:53:02|
|---|-------------|--------------------|
|2  |Offline      |'2017-09-07 12:54:00|
|---|-------------|--------------------|
|2  |Online       |'2017-09-07 12:54:01|
|---|-------------|--------------------|
|3  |Offline      |'2017-09-07 12:54:02|
|---|-------------|--------------------|
|1  |Online       |'2017-09-07 12:55:21|
|---|-------------|--------------------|
|2  |Offline      |'2017-09-07 12:57:21|
|---|-------------|--------------------|
|2  |Online       |'2017-09-07 12:58:21|
这是我需要的结果表,顺序无关紧要。时间差可以是秒

|ID |Time Difference |
|---|----------------|
|1  |141             |
|---|----------------|
|2  |1               |
|---|----------------|
|2  |60              |

在数据中,联机/脱机状态是交错的。这使得铅的使用变得简单:


在数据中,联机/脱机状态是交错的。这使得铅的使用变得简单:


试试这段代码,你可以减少代码的大小,也可以提高性能。。但是循环工作正常,用表名替换t。希望对你有用


试试这段代码,你可以减少代码的大小,也可以提高性能。。但是循环工作正常,用表名替换t。希望对你有用


答案对你有用吗?谢谢。这个答案对你有用吗?谢谢
select id,
       sum(datediff(second, time, next_t)
from (select t.*,
             lead(description) over (partition by id order by time) as next_d,
             lead(time) over (partition by id order by time) as next_t
      from t
     ) t
where description = 'Offline' and next_d = 'Online';
declare @idTable table (i_idx int identity(1,1), id int)
declare @Result table (id int, Time_diff int)
CREATE  table  #Auxtable (i_idx int identity(1,1), id int, Description varchar(50), Time datetime)

insert into @idTable 
select distinct(id) from t

declare @id int
declare @idx int
declare @idx2 int
declare @count int
declare @Offline int
declare @Online int
declare @AuxCount int
declare @auxtableCount int
declare @Description varchar(50)
declare @DescriptionNext varchar(50)
declare @time datetime
declare @timenext datetime

set @idx = 1
set @idx2 = 1
set @count = (select count(*) from @idTable)

while (@idx < @count)
begin
    set @id = (select id from @idTable where i_idx = @idx)
    set @Offline = (select count(*) from t where Description = 'Offline' and id = @idx)
    set @Online = (select count(*) from t where Description = 'Online' and id = @idx)

    if(@Offline > 0 And @Online > 0)
    begin
        insert into #Auxtable select id, Description, Time from t where id = @id order by Time
        set @auxtableCount = (select count(*) from #Auxtable)

        while(@idx2 <= @auxtableCount)
        begin   
            set @Description = (select top 1 Description from #Auxtable where i_idx = @idx2)
            set @DescriptionNext = (select Description from #Auxtable  where i_idx = @idx2 + 1)
            set @time = (select time from #Auxtable where i_idx = @idx2)
            set @timenext = (select time from #Auxtable where i_idx = @idx2 + 1)

            if(@Description = 'Offline' and @DescriptionNext = 'Online')
            begin
                insert into @Result (id, Time_diff) values (@id, datediff(second, @time, @timenext))
                set @idx2 = @idx2 + 1
            end

            set @idx2 = @idx2 + 1
        end

    end
    TRUNCATE TABLE #Auxtable
    SET @idx2 = 1

    set @idx = @idx + 1
end

DROP TABLE #Auxtable

SELECT * FROM @Result