2017-09-08 3 views
1

私はテーブルを持っていますが、短縮バージョンを以下に示します。私は各IDがオフラインになる時間を見つける必要があります。 IDがオンラインにならない場合でも、それは無視されるべきで、各IDは、オフラインで複数回行って、それがため、これは私が必要とする結果のテーブルでオフライン2つの特定のSQL行の時間差を見つけて新しいテーブルに移入する

|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    | 
+0

答えはあなたのために働いていましたか?ありがとう。 –

答えて

0

データには、オンライン/オフライン状態がインターリーブされています。これはlead()を使用することが容易になります:あなたのテーブル名を指定して(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'; 
0

を使用すると、コードサイズを縮小し、同様に向上させることができ、このコードを試してみてください..しかし、ループが正常に動作しています。あなたのために働くことを願っています。

Select Result

table

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 
関連する問題