Advertisement
imk0tter

Untitled

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