2015-06-30 52 views
1

我有SQL Server 2008中票據歷史記錄的日誌表。這裏我只有日期時間和狀態字段。我需要計算狀態之間的總時間。計算SQL Server 2008中票據使用的總時間量

例子:
我在2015-06-19 15:50:44.000開了票一段時間後我把它正在進行在2015-06-22 11:15:15.000改爲工作。兩個時間戳值都將在相同的DateTime字段下。我想計算狀態更改之間的時間差。狀態將隨機更改。

ticket_number Date & Time   Problem Status 
16676  2015-06-19 15:50:14.000 Open 
16676  2015-06-19 15:50:14.000 Accepted 
16676  2015-06-19 15:50:44.000 Work in progress 
16676  2015-06-19 16:03:13.000 Pending Vendor 
16676  2015-06-22 06:32:31.000 Work in progress 
16676  2015-06-22 11:15:15.000 Pending Vendor 
16676  2015-06-23 10:15:15.000 Work in progress 
16676  2015-06-23 10:15:15.000 Closed 

在這裏我必須計算從接受狀態到關閉狀態的總時間,我們必須排除未決的供應商狀態時間。

在前提前感謝

+0

編輯使問題更清楚。 –

+0

請問哪個版本?從SQL Server 2012開始,您有這樣的查詢的'LAG'語句。 –

回答

0

如果每票只有2個條目,那麼這非常簡單。既然你沒有張貼任何樣本數據或模式信息,我們必須在這裏作出一些假設,但例如:

Select op.ticket_number 
     , datediff(minute, wip.timestamp, op.timestamp) as diff_minutes 
from yourTable op 
     inner join yourTable wip 
      on op.ticket_number = wip.ticket_number 
where wip.status = 'Work_in_progress' 
and op.status = 'Opened' 
order by op.ticket_number 

這將需要的地方的狀態被打開「OP」在表中的記錄集以及狀態爲「正在進行中」的數據的另一個子集稱爲「wip」。如果您將這些加入到票號中,您可以計算兩個相應時間戳之間的差異(以小時,天,秒等爲單位)。 如果你有兩個以上的狀態來處理這個問題會變得複雜一些,那麼其他方法可能會更合適。在這種情況下,請張貼一些示例數據和相關表格的模式。

+0

**請注意:**從SQL Server 2012開始,您擁有**'LAG' **語句,但不進行自連接也是這樣:https://msdn.microsoft.com/en-us /library/hh231256.aspx –

+0

的確,但問題的標題說明用戶正在使用MSSQL 2008,所以LAG不適合他。 –

+0

標籤之後進行了編輯。 [* SQLPolice從未投票*] –

0

添加此作爲單獨的答案,因爲方法是完全不同的。很肯定這是你在找什麼:

-- Create table for sample data 
Create table #tickets 
( ticket_number int 
, dateTime_status datetime 
, ProblemStatus nvarchar(50) 
) 

-- Insert sample data into temp table 
Insert into #tickets 
Values (16676, '2015-06-19 15:50:14.000', 'Open') 
    , (16676, '2015-06-19 15:50:14.000', 'Accepted') 
    , (16676, '2015-06-19 15:50:44.000', 'Work in progress') 
    , (16676, '2015-06-19 16:03:13.000', 'Pending Vendor') 
    , (16676, '2015-06-22 06:32:31.000', 'Work in progress') 
    , (16676, '2015-06-22 11:15:15.000', 'Pending Vendor') 
    , (16676, '2015-06-23 10:15:15.000', 'Work in progress') 
    , (16676, '2015-06-23 10:15:15.000', 'Closed') 
    , (16677, '2015-06-19 15:50:14.000', 'Open') 
    , (16677, '2015-06-20 15:50:14.000', 'Accepted') 
    , (16677, '2015-06-20 15:50:44.000', 'Work in progress') 
    , (16677, '2015-06-20 16:03:13.000', 'Pending Vendor') 
    , (16677, '2015-06-23 06:32:31.000', 'Work in progress') 
    , (16677, '2015-06-23 11:15:15.000', 'Pending Vendor') 
    , (16677, '2015-06-24 10:15:15.000', 'Work in progress') 
    , (16677, '2015-06-25 10:15:15.000', 'Closed') 

-- Select time difference for status from and to per ticket 
-- Time difference is measured in minutes. Change the datepare to desired unit if required. 
Select  ROW_NUMBER() over(partition by a.ticket_number order by a.RowNum) 
     , a.ticket_number 
     , a.ProblemStatus as 'Status_from' 
     , b.ProblemStatus as 'Status_to' 
     , DATEDIFF(MINUTE,a.dateTime_status,b.dateTime_status) as 'Time_in_minutes' 
from  
     -- Nested select to set subsequent events side by side. Add row numbers. 
     (Select  ROW_NUMBER() over(partition by ticket_number order by dateTime_status) as 'RowNum' 
       , ticket_number 
       , dateTime_status 
       , ProblemStatus 
     from #tickets) as A 

     -- Join first nested query on second one with same data, but join on b.rownum = a.rownum + 1 
     -- This puts the event in B next to the previous event in A 
     inner join (Select ROW_NUMBER() over(partition by ticket_number order by dateTime_status) as 'RowNum' 
         , ticket_number 
         , dateTime_status 
         , ProblemStatus 
        from #tickets) as B 
      on a.ticket_number = b.ticket_number 
      and b.RowNum = a.RowNum + 1 
-- Exclude records where the beginning status is "Pending Vendor" 
where a.ProblemStatus <> 'Pending Vendor' 
order by a.ticket_number 
     , a.RowNum 

-- Drop the temp table 
drop table #tickets 

你基本上還加入相同的數據彼此爲2個獨立的數據集,但通過添加行號的數據,你可以加入1分表至第二個表中的下一行。這允許您並排設置順序事件,並計算它們之間的時間差。排除第一個表中的ProblemStatus爲「待定供應商」的任何記錄,並且您有實際的時間處理該問題。分鐘總數是您每張票的總時間。如果您需要更準確的時間測量,請將日期部分更改爲秒。然後很容易將其轉換爲秒,分鐘,小時格式。

+0

謝謝。你救了我的命。這是非常有幫助 – user2845419

+0

很高興我能幫到你。 –