| Get Rows • Join tickets to dates table to get a row/day of possible breach dates Calc hours • Running total of SLA hours used by the end of each day Find breach • SLA hours between current and previous running totals
| Get Rows for Possible Breach Dates select … from tickets t join calendar_dates d on trunc ( raised_datetime ) <= calendar_date and raised_datetime + 20 > calendar_date join priorities p on t.priority_id = p.priority_id
| Get Rows for Possible Breach Dates select … from tickets t join calendar_dates d on trunc ( raised_datetime ) <= calendar_date and raised_datetime + 20 > calendar_date join priorities p on t.priority_id = p.priority_id > max elapsed days to breach
| sum ( … ) over ( partition by ticket_id order by calendar_date ) * 24 tot, sum ( … ) over ( partition by ticket_id order by calendar_date rows between unbounded preceding and 1 preceding ) * 24 prev_tot Calculate Hours Used
| sum ( … ) over ( partition by ticket_id order by calendar_date ) * 24 tot, sum ( … ) over ( partition by ticket_id order by calendar_date rows between unbounded preceding and 1 preceding ) * 24 prev_tot Calculate Hours Used Hours used by day end
| sum ( … ) over ( partition by ticket_id order by calendar_date ) * 24 tot, sum ( … ) over ( partition by ticket_id order by calendar_date rows between unbounded preceding and 1 preceding ) * 24 prev_tot Calculate Hours Used Hours used at day start
| "…a code grinder is going to figure it out, while a database programmer is going to spell it out" - Ken Downs https://database-programmer.blogspot.com/2007/11/database-skills-introdution.html
| How to Find the Next Business Day and Add or Subtract N Working Days with SQL https://bit.ly/next-working-day-sql Ask TOM: Adding hours to date, but within boundaries https://bit.ly/add-business-hours Further Reading