2014-11-15 110 views
-1

我正在計算票證系統中兩個日期之間的時差。在主表中我有RowID,TicketID,TicketCategory,CreatedMoment。當票證解析後,它將顯示在Statuschange列中的另一個表上,並且兩個表中的公共字段都是RowID。實施例見下表:計算兩個日期之間的時差

表1

RowID TicketID TicketCategory Createdmoment  Status 
1234  11223344 PC Installation 12 Nov 2014 0800 1 
1235  11223355 Server Config  12 Nov 2014 0930 1 

表2

TicketRowID Statuschange  OldStatus NewStatus 
1234   13 Nov 2014 1300 1   2 
1235   13 Nov 2014 1100 1   2 

在上述例子中的票券在上午11時和下午1時,第二天都解決了,在報告我正在我計算第二天中午12點之前解決的票數,因此在上面的示例報告中將顯示我已經關閉了1張票並且1正在等待。我必須運行上個月的數據查詢以日常格式顯示。需要幫助正確的代碼。

回答

0

希望下面的代碼片段將幫助:

declare @tab table (RowID int, TicketID varchar(100), TicketCategory varchar(100) 
, Createdmoment datetime, [Status] int) 

insert into @tab 
select 1234,'11223344','PC Installation','12 Nov 2014 08:00',1 
union 
select 1235,'11223355','Server Config','12 Nov 2014 09:30',1 


declare @tab2 table (TicketRowID int, Statuschange datetime, OldStatus int, NewStatus int) 
insert into @tab2 
select 1234, '13 Nov 2014 13:00',1, 2 
union 
select 1235, '13 Nov 2014 11:00', 1, 2 

select *, dateadd(HH, 12, convert(datetime, cast(dateadd(day,1,t1.Createdmoment) as date))) NextDay12PM from @tab t1 
select * from @tab2 t2 

select t1.RowID, t1.TicketID, t1.TicketCategory, t1.Createdmoment, t2.Statuschange as 'Closedmoment', NewStatus 
, case when t2.Statuschange < dateadd(HH, 12, convert(datetime, cast(dateadd(day,1,t1.Createdmoment) as date))) then sum(1) else sum(0) end as 'Resolved' 
, case when t2.Statuschange > dateadd(HH, 12, convert(datetime, cast(dateadd(day,1,t1.Createdmoment) as date))) then sum(1) else sum(0) end as 'Pending' 
from @tab t1 
left join @tab2 t2 on t2.TicketRowID = t1.RowID 
group by t1.RowID, t1.TicketID, t1.TicketCategory, t1.Createdmoment, NewStatus, t2.Statuschange 

感謝。

相關問題