Solved

SQL Datediff function returning minimum value of 1

Posted on 2013-05-28
2
569 Views
Last Modified: 2013-05-28
So I am tracking the average number of days our troublecall tickets are open. Now with the following code i can get close, but my problem that I found is that if a ticket is opened and closed on the same day the Datediff function counts it as 0 days open instead of 1. Is there a better function to use or is there a way to have datediff fulfill the job I need it to do?

Here is the code:

SELECT      DATEPART(iso_week,Closed)as 'ISO Week'
      , AVG(Datediff(DAY,Created,Closed))as 'Avg days to Close'       
      , COUNT(closed)as Closed
FROM TSL
WHERE Priority < 7 and Closed > GETDATE()-273
GROUP BY YEAR(CLOSED), DATEPART(iso_week,Closed)
ORDER BY YEAR(CLOSED), DATEPART(iso_week,Closed)
0
Comment
Question by:NCSA SCADA
2 Comments
 
LVL 22

Accepted Solution

by:
Thomasian earned 500 total points
ID: 39201891
, AVG(CASE WHEN Datediff(DAY,Created,Closed)=0 THEN 1 ELSE Datediff(DAY,Created,Closed) END)as 'Avg days to Close'
0
 

Author Closing Comment

by:NCSA SCADA
ID: 39202002
Thanks for the quick response. that makes total sense now that i see the code. you saved me alot of time
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now