Advertisement
imk0tter

Untitled

Feb 14th, 2012
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 0.91 KB | None | 0 0
  1. SELECT
  2.     A.FirstName,
  3.     A.LastName,
  4.     A.EmployeeID,
  5.     COALESCE(C.PhonesToday,0) AS PhonesToday,
  6.     AVG(B.AvgTimePerPhone) AS AvgTimePerPhone,
  7.     AVG(B.PhonesPerDay) AS AvgPhonesPerDay,
  8. FROM
  9.     (
  10.         EmployeeTable AS A
  11.         INNER JOIN
  12.         (
  13.             SELECT
  14.                 A.EmployeeID,
  15.                 AVG(A.Timer) AS AvgTimePerPhone,
  16.                 COUNT(DISTINCT A.AuditID) AS PhonesPerDay,
  17.                 DATEADD(dd,0, DATEDIFF(dd,0,A.AuditDate)) AS AuditDate
  18.             FROM
  19.                 AuditTable AS A
  20.             GROUP BY
  21.                 A.EmployeeID,
  22.                 AuditDate    
  23.         ) AS B
  24.         ON A.EmployeeID = B.EmployeeID
  25.     )
  26.     LEFT JOIN
  27.     (
  28.         SELECT
  29.             A.EmployeeID,
  30.             COUNT(A.AuditID) AS PhonesToday
  31.         FROM
  32.             AuditTable AS A
  33.         WHERE
  34.             DATEADD(dd,0, DATEDIFF(dd,0,A.AuditDate)) = DATEADD(dd,0,DATEDIFF(dd,0,GETDATE()))
  35.         GROUP BY
  36.             A.EmployeeID
  37.     ) AS C
  38.     ON A.EmployeeID = C.EmployeeID
  39. GROUP BY
  40.     A.FirstName,
  41.     A.LastName,
  42.     A.EmployeeID,
  43.     PhonesToday
  44. ORDER BY
  45.     PhonesToday DESC, AvgPhonesPerDay DESC
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement