1

Thanks in advance!!

Here is what I have so far:

I am selecting the total amount of tasks by person for a specific owner for a given date range. I am now trying to get the exact daily average for number of tasks for each person.

The problem I am having is that it keeps returning a .00 on the end of the average, rather than the exact average. For example, a person may have 2 tasks in a week; I am getting 0.00 for the average rather than 0.28 or 0.29

SELECT 
   convert(nvarchar, COUNT(p.personID)) AS count,
   CONVERT(decimal(4, 2), COUNT(p.personID) / DATEDIFF(DAY, @startDate, @endDate)) AS average,
   p.personID,
   p.firstname,
   p.lastname,
   c.companyname
FROM   
   Tasks t
JOIN 
   Person p ON p.personID = t.personID
JOIN 
   Client c ON c.id = p.employer
JOIN
   Commission m ON m.ClientID = c.ID
WHERE  
   t.created BETWEEN @startDate AND @endDate
   AND m.owner IN ('John Doe')
GROUP BY 
   p.personID, p.firstname, p.lastname, c.companyname
ORDER BY 
   c.companyname, count DESC
4

1 回答 1

2

Be aware that if enddate and startdate are identical you get a division by 0 error.

Your problem was that COUNT(p.personID) and DATEDIFF(DAY,@startDate,@endDate) both return integers. When dividing 2 integers you get a third integer rounded Down. By multiplying by 1.0 you get a decimal value.

CONVERT(decimal(4,2),1.0*COUNT(p.personID)/DATEDIFF(DAY,@startDate,@endDate))
于 2013-08-07T19:17:14.177 回答