Advertisement
imk0tter

Untitled

Feb 14th, 2012
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 0.88 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.InventoryID) AS PhonesPerDay,
  17.                 DATEADD(dd,0, DATEDIFF(dd,0,A.ReceiveDate)) AS ReceiveDate
  18.             FROM
  19.                 InventoryTable AS A
  20.             GROUP BY
  21.                 A.EmployeeID,
  22.                 ReceiveDate  
  23.         ) AS B
  24.         ON A.EmployeeID = B.EmployeeID
  25.     )
  26.     LEFT JOIN
  27.     (
  28.         SELECT
  29.             A.EmployeeID,
  30.             COUNT(A.InventoryID) AS PhonesToday
  31.         FROM
  32.             InventoryTable AS A
  33.         WHERE
  34.             DATEADD(dd,0, DATEDIFF(dd,0,A.ReceiveDate)) = 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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement