Hi
This is ugly, but it seems to work ... I think.
There is bound to be a more elegant and efficient way to do this:-)
WITH tryThis as (
SELECT s.workdate, s.businessunit
,s.totalcontacts, s.SIH, s.SLEligible, s.InServiceLevel
,h.workingdate
,row_number() OVER (partition by h.businessunit order by calendardate) N1
,row_number() OVER (partition by h.businessunit, workingdate order by calendardate) N2
FROM #SLHIH s
INNER JOIN #HoursOfOperation h ON h.CalendarDate = s.workdate and h.businessunit = s.businessunit
)
SELECT a.WorkDate, a.BusinessUnit,
a.totalcontacts, b.totalcontacts,
a.SIH, b.SIH,
a.SLEligible, b.SLEligible,
a.InServiceLevel, b.InServiceLevel
--UPDATE a
--SET a.totalcontacts = b.totalcontacts,
--a.SIH = b.SIH,
--a.SLEligible = b.SLEligible,
--a.InServiceLevel = b.InServiceLevel
FROM tryThis a
INNER JOIN tryThis b ON b.N2 = a.N1 - a.N2 and a.workingdate = 0 and b.workingdate = 1 and a.businessunit = b.businessunit
ORDER BY a.BusinessUnit, a.WorkDate