2017-06-01 9 views
0

SQLのレコード間の時間の計測にはいくつか問題があります。 私の場合のように、アイテムがアクティブでなかった期間。データのSQLの他の列の値に基づく2つの値の時差

サンプル:

Item Not_Active Active Created 
A  0   1  2017-01-01 10:03:15 
A  1   0  2017-01-01 12:05:55 
A  0   1  2017-01-01 13:05:55 
A  1   0  2017-02-03 12:05:55 
A  0   1  2017-02-05 13:05:55 
B  1   0  2017-01-10 04:05:20 
B  0   1  2017-01-10 07:05:20 

だから主な目標は、合計に各項目ための無効化と活性化との間のすべての時間です。答えは数秒でなければなりません。どのような種類のクエリを使用する必要がありますか?

チャレンジ2:特定の日付間のアクティブ時間をカウントしません。たとえば、2017-02-01と2017-02-28の間の項目Aのみです。

チャレンジ3:アクティブな時間が夜間にどのくらいであったかをカウントすると、午前0時から午前5時までと言います。

私はどこから始めたらよいか分かりません。助言がありますか?

+0

視覚的な結果は、スキーマと併せて役立ち、正しいデータベースにフラグを立てることもできます – maSTAShuFu

+0

どのDBMSを使用していますか? Postgres?オラクル? DB2?ファイアバード? –

+0

T-SQLでは、DATEDIFFを使用して、任意のビジネスロジックに基づいて日付にロジックを送信します。 –

答えて

0

私はあなたがこれをどのように作成できるか少しサンプルを作成します。

create table #table (item char, not_active int, active int, created datetime) 

    insert into #table values('A', 0, 1, '2017-01-01 10:03:15') 
    insert into #table values('A', 1, 0, '2017-01-01 12:05:55') 
    insert into #table values('A', 0, 1, '2017-01-01 13:05:55') 
    insert into #table values('A', 1, 0, '2017-02-03 12:05:55') 
    insert into #table values('A', 0, 1, '2017-02-05 13:05:55') 
    insert into #table values('B', 1, 0, '2017-01-10 04:05:20') 
    insert into #table values('B', 0, 1, '2017-01-10 07:05:20') 

    declare @very_hard table (item char, 
          not_active int, 
          active int, 
          created datetime, 
          before datetime, 
          afters datetime, 
          days_diff_stoped int, 
          hourstoped int) 

declare very_hard_challenge cursor Local 
for 

select 
item, 
active, 
not_active, 
created, 
LAG(created,1,0)over(partition by item order by created) as before, 
LEAD(created,1,0)over(partition by item order by created) as afters, 
datediff(day,LAG(created,1,0)over(partition by item order by created),created) as days_diff_stoped 
from #table 
--where item = 'B' 
order by created asc 

declare @item char, 
     @not_active int, 
     @active int, 
     @created datetime, 
     @days_diff_stoped int, 
     @before datetime, 
     @afters datetime 

open very_hard_challenge 

fetch next from very_hard_challenge into @item, @not_active, @active, @created, @before, @afters, @days_diff_stoped 

WHILE @@FETCH_STATUS=0 
    begin 
     declare @datebegin datetime = @before 
     declare @hourstoped int = 0 
     if @datebegin != '1900-01-01 00:00:00.000' 
     begin 
      while @datebegin < convert(datetime,@created) 
       begin 
         if datepart(hour,@datebegin) in(0,1,2,3,4,5) 
          begin 
           set @hourstoped = @hourstoped+1 
          end 

          set @datebegin = DATEADD(hour,1,@datebegin) 
       end 
     end 

     insert into @very_hard values (@item, @not_active, @active, @created, @before, @afters, @days_diff_stoped, @hourstoped) 

     fetch next from very_hard_challenge into @item, @not_active, @active, @created, @before, @afters, @days_diff_stoped 
    end 

close very_hard_challenge 
deallocate very_hard_challenge 

select * from @very_hard 

このコードはシンプルですが、改善することができます。誰でもこのコードについて質問すれば教えてください。私は答えを可能にします。

+0

ありがとうございました。それは働いています:) :) – Skaiste

+0

この回答が役立つ場合は、チェックマークをクリックして受け入れてください。この回答は他の人に役立つことがあります。 ;) –

+0

確かに! :) Igor、多分あなたはまた、最後の挑戦で私を助けることができます - ちょうど夜の時間帯を数える方法? :/ – Skaiste

関連する問題