socan
asked on
Create a SQL function to calculate "datediff" based on hours of operation
We have a helpdesk ticketing software database on a SQL 2000 server. We are trying to query to see if an SLA has passed based on the priority of the ticket, but we want this calculation based on 7am - 7 pm Mon - Fri. We were thinking we would have to create a function for this, but don't know how to go about it. Any ideas?
example: a call is logged on wednesday at 6pm and is resolved on thursday at 8:00am the difference shoudl be 2 hours, not 14. If the priority was a 2 (4 hour SLA) then the SLA would be met.
example 2: a call was logged on friday at 2pm and resolved on monday at 10am. Priority 3 is one business day, so it should be met.
example: a call is logged on wednesday at 6pm and is resolved on thursday at 8:00am the difference shoudl be 2 hours, not 14. If the priority was a 2 (4 hour SLA) then the SLA would be met.
example 2: a call was logged on friday at 2pm and resolved on monday at 10am. Priority 3 is one business day, so it should be met.
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
The sqlteam link looks like it may be helpful but doesn't seem to account for hours, and it is a little confusing - it will require a longer sit down to absorb. As for mark's comment: we have 5 priorities (priority 5 has no SLA - so there are 4 SLA levels). P1 runs on 24 hour clock so no special calculations are needed. Calculating P3 as 24 hours and working only with the dates could possibly work, and something similar could be done with P4, but P2 would require a different approach. If we just leave the hours as they are, a call opened at 6pm would briech at 10pm (instead of 10am the next day). I'd also like to be able to adjust the hours of operation or the SLA levels easily in the future.
P1 = 2 hours
P2 = 4 hours
P3 = 1 day
P4 = 3 days
P5 = no SLA
P1 = 2 hours
P2 = 4 hours
P3 = 1 day
P4 = 3 days
P5 = no SLA
OK, no problems - might do the function rather than pure query - it can be the home for "hours" and the date arithmetic will get a bit "ordinary" when checking for weekends and day flowovers if we try to do it inline within a query itself .
One question though, do you have a formal home for Priority with Hours in it already ?
How about the outcome of the query - if we express in terms of "TIME REMAINING" then positive numbers will be "within" SLA and negative numbers will be "overdue" or exceeding the SLA.
Sound OK ?
One question though, do you have a formal home for Priority with Hours in it already ?
How about the outcome of the query - if we express in terms of "TIME REMAINING" then positive numbers will be "within" SLA and negative numbers will be "overdue" or exceeding the SLA.
Sound OK ?
ASKER
My original idea was to use a combination of a function and queries. For maximum flexibility, I think we should follow this scheme:
A function would compare two dates and return the difference, minus non-business hours
(to determine business hours it would consult a table or be defined internally in the function)
The query would use the results of the function to determine if an SLA had been breached based on the priority, etc.
A table for business hours would be good because then we could get pretty granular like start and end times for each day of the work week and excluded (holiday) days. We could do the same with declared variables inside the function.. it would make it tighter but less flexible.
To answer your question, currently there is no "home" for hours, or priorities, or business days other than in the ticketing application itself, which is unusable from this end.
I suppose it would also be possible to pass the working days / hours to the function as a parameter, but then we'd have to keep it pretty simple and would never be able to account for holidays at all.
A function would compare two dates and return the difference, minus non-business hours
(to determine business hours it would consult a table or be defined internally in the function)
The query would use the results of the function to determine if an SLA had been breached based on the priority, etc.
A table for business hours would be good because then we could get pretty granular like start and end times for each day of the work week and excluded (holiday) days. We could do the same with declared variables inside the function.. it would make it tighter but less flexible.
To answer your question, currently there is no "home" for hours, or priorities, or business days other than in the ticketing application itself, which is unusable from this end.
I suppose it would also be possible to pass the working days / hours to the function as a parameter, but then we'd have to keep it pretty simple and would never be able to account for holidays at all.
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Oh, one more thing, the above loses flexibility of variable start and end times by individual day, but does take into account holidays, weekends etc...
priority = 2 = 4 hours
priority = 3 = 24 hours (easy date arithmetic - just weekends)