2017-06-01 71 views
0

我在計算SQL中記錄之間傳遞的時間時遇到了一些問題。 或者就我的情況而言,該物品沒有活動多久。基於SQL中其他列值的兩個值之間的時間差

樣本數據:

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 

所以主要目標是總和爲每個項目激活和激活之間的所有時間。答案應該在幾秒鐘內。我應該使用哪種查詢?

挑戰二:計算某些日期之間沒有活動時間。例如,在2017-02-01和2017-02-28之間的項目A.

挑戰三:計算多少不活動時間是在夜間,可以說在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

當然! :)伊戈爾,也許你還可以幫助我與最後的挑戰 - 如何計算一個晚上的時間? :/ – Skaiste

相關問題