SELECT E.empnum, RTRIM(C.lname) + ', ' + RTRIM(C.fname) AS Name,
RTRIM(SUBSTRING(C.dept,1,3)) AS Office,
C.dept,
calldate,
duration_in_decimal,
io
FROM CALLS C
INNER JOIN ( -- subquery returnig beginning of the curent(?) month. This approach allows us to do the conversion and month calculations just once
SELECT DATEADD(mm, DATEDIFF(mm, 0, CONVERT(DATETIME, '20121130', 112)), 0) MonthStart ) CM
-- selecting the range of dates for calldate - the way allowing to use an index
ON calldate >= DATEADD(mm, -1, MonthStart) AND calldate < MonthStart
INNER JOIN EMP E ON C.empnum = E.empnum
WHERE C.empnum = 6396 -- following Sean's sound advice
AND (
-- now the conditions look pretty straight forward. Please correct it if I made a mistake somewhere.
-- it was not so easy to find the ends in the original version :-)
(duration_in_decimal > .0055555 AND LEN(phonenum) IN (7,10,11))
OR io = 'I'
)
ORDER BY C.empnum;
And one last comment.
I'm pretty sure the table CALLS has a clustered index with 1st column "calldate". Right?
Well, why am I asking? It just cannot be any other way.
Silly me.:hehe:
_____________
Code for TallyGenerator