希望下面的代碼片段將幫助:
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
感謝。