asked on
;With Hours_cte as
(
select t1.Ticketid, t1.AssignedtoteamId, isnull(t2.userassignmentactivity,t1.userassignmentactivity) as activity,isnull(t2.userassignmentactivity,'No Details') as assignedto,
case when datename(dw ,isnull(t2.lastupdateddtim,t1.lastupdateddtim)) = 'Saturday' and datepart(hour,isnull(t2.lastupdateddtim,t1.lastupdateddtim)) > 18 then dateadd(hour,18 - datepart(hour,isnull(t2.lastupdateddtim,t1.lastupdateddtim)),isnull(t2.lastupdateddtim,t1.lastupdateddtim))
when datename(dw ,isnull(t2.lastupdateddtim,t1.lastupdateddtim)) = 'Sunday' then dateadd(hour,-24 + 18 - datepart(hour,isnull(t2.lastupdateddtim,t1.lastupdateddtim)),isnull(t2.lastupdateddtim,t1.lastupdateddtim))
when datename(dw ,isnull(t2.lastupdateddtim,t1.lastupdateddtim)) = 'Monday' and datepart(hour,isnull(t2.lastupdateddtim,t1.lastupdateddtim)) > 18 then dateadd(hour,-48 + 18 - datepart(hour,isnull(t2.lastupdateddtim,t1.lastupdateddtim)),isnull(t2.lastupdateddtim,t1.lastupdateddtim))
else isnull(t2.lastupdateddtim,t1.lastupdateddtim)
end as adjusted_date,
row_number() over (partition by t1.ticketid, t1.assignedtoteamid order by t2.lastupdateddtim) as RN
from #table1 t1
left outer join #table2 t2 on t1.ticketid = t2.ticketid and t1.assignedtoteamid = t2.previousteamid
where t1.status = 'active'
AND not exists (select NULL from #table2 t2x where t2x.ticketid = t2.ticketid and t2x.previousteamid <> t2.previousteamid and t2x.lastupdateddtim > t2.lastupdateddtim)
)
Select h1.ticketid, h1.assignedtoteamid, h1.activity, h1.assignedto, h1.adjusted_date, isnull(h2.adjusted_date,getdate()) as adjusted_end,
((datepart(wk,isnull(h2.adjusted_date,getdate())) - datepart(wk,h1.adjusted_date)) * 72) as non_working_hours,
case when h1.assignedto in ('approval','work') then datediff(hour, h1.adjusted_date, isnull(h2.adjusted_date,getdate())) else 0 end as time_not_counted,
datediff(hour, h1.adjusted_date, isnull(h2.adjusted_date,getdate())) as time_difference,
datediff(hour, h1.adjusted_date, isnull(h2.adjusted_date,getdate()))
- case when h1.assignedto in ('approval','work') then datediff(hour, h1.adjusted_date, isnull(h2.adjusted_date,getdate())) else 0 end
- ((datepart(wk,isnull(h2.adjusted_date,getdate())) - datepart(wk,h1.adjusted_date)) * 72) as working_hours
from Hours_cte h1
left outer join hours_cte h2 on h1.ticketid = h2.ticketid and h1.assignedtoteamid = h2.assignedtoteamid and h1.rn + 1 = h2.rn
;With Hours_cte as
(
select t1.Ticketid, t1.AssignedtoteamId, isnull(t2.userassignmentactivity,t1.userassignmentactivity) as activity,isnull(t2.userassignmentactivity,'No Details') as assignedto,
case when datename(dw ,isnull(t2.lastupdateddtim,t1.lastupdateddtim)) = 'Saturday' and datepart(hour,isnull(t2.lastupdateddtim,t1.lastupdateddtim)) > 18 then dateadd(hour,18 - datepart(hour,isnull(t2.lastupdateddtim,t1.lastupdateddtim)),isnull(t2.lastupdateddtim,t1.lastupdateddtim))
when datename(dw ,isnull(t2.lastupdateddtim,t1.lastupdateddtim)) = 'Sunday' then dateadd(hour,-24 + 18 - datepart(hour,isnull(t2.lastupdateddtim,t1.lastupdateddtim)),isnull(t2.lastupdateddtim,t1.lastupdateddtim))
when datename(dw ,isnull(t2.lastupdateddtim,t1.lastupdateddtim)) = 'Monday' and datepart(hour,isnull(t2.lastupdateddtim,t1.lastupdateddtim)) > 18 then dateadd(hour,-48 + 18 - datepart(hour,isnull(t2.lastupdateddtim,t1.lastupdateddtim)),isnull(t2.lastupdateddtim,t1.lastupdateddtim))
else isnull(t2.lastupdateddtim,t1.lastupdateddtim)
end as adjusted_date,
row_number() over (partition by t1.ticketid, t1.assignedtoteamid order by t2.lastupdateddtim) as RN
from #table1 t1
left outer join #table2 t2 on t1.ticketid = t2.ticketid and t1.assignedtoteamid = t2.previousteamid
where t1.status = 'active'
AND not exists (select NULL from #table2 t2x where t2x.ticketid = t2.ticketid and t2x.previousteamid <> t2.previousteamid and t2x.lastupdateddtim > t2.lastupdateddtim)
)
Select h1.ticketid, h1.AssignedtoteamId,
sum (datediff(hour, h1.adjusted_date, isnull(h2.adjusted_date,getdate()))
- case when h1.assignedto in ('approval','work') then datediff(hour, h1.adjusted_date, isnull(h2.adjusted_date,getdate())) else 0 end
- ((datepart(wk,isnull(h2.adjusted_date,getdate())) - datepart(wk,h1.adjusted_date)) * 72)) as working_hours
from Hours_cte h1
left outer join hours_cte h2 on h1.ticketid = h2.ticketid and h1.assignedtoteamid = h2.assignedtoteamid and h1.rn + 1 = h2.rn
group by h1.Ticketid, h1.AssignedtoteamId
ASKER
ASKER
ASKER
ASKER
Microsoft SQL Server is a suite of relational database management system (RDBMS) products providing multi-user database access functionality.SQL Server is available in multiple versions, typically identified by release year, and versions are subdivided into editions to distinguish between product functionality. Component services include integration (SSIS), reporting (SSRS), analysis (SSAS), data quality, master data, T-SQL and performance tuning.
TRUSTED BY