Advertisement
imk0tter

Untitled

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