Advertisement
imk0tter

Untitled

Feb 18th, 2012
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 1.34 KB | None | 0 0
  1.         SELECT
  2.             D.PartID,
  3.             D.PartType,
  4.             A.EntryDate,
  5.             COALESCE(SUM(B.UsedPartCount),9) AS UsedPartCount,
  6.             COALESCE(SUM(C.UnsortedPartCount),0) AS UnsortedPartCount
  7.         FROM
  8.         DateTable AS A
  9.         LEFT JOIN
  10.         (
  11.             SELECT
  12.                 COALESCE(COUNT(A.JobEntryPartID),0) AS UsedPartCount,
  13.                 A.PartID,
  14.                 DATEADD(dd,0,DATEDIFF(dd,0,B.RepairDate)) AS EntryDate
  15.             FROM
  16.                 JobEntryPartTable AS A
  17.                 INNER JOIN
  18.                 JobEntryTable AS B
  19.                 ON A.JobEntryID = B.JobEntryID
  20.                 INNER JOIN
  21.                 JobPartTypeTable AS C
  22.                 ON A.PartType = C.PartType
  23.             WHERE
  24.                 A.PartID > 1
  25.                 AND
  26.                 C.Remove = 1
  27.             GROUP BY
  28.                 A.PartID, EntryDate
  29.         ) AS B
  30.         ON A.EntryDate = B.EntryDate
  31.         LEFT JOIN
  32.         (
  33.             SELECT
  34.                 COUNT(A.JobEntryPartID) AS UnsortedPartCount,
  35.                 A.PartID,
  36.                 DATEADD(dd,0,DATEDIFF(dd,0,B.RepairDate)) AS RepairDate
  37.             FROM
  38.                 JobEntryPartTable AS A
  39.                 INNER JOIN
  40.                 JobEntryTable AS B
  41.                 ON A.JobEntryID = B.JobEntryID
  42.                 INNER JOIN
  43.                 JobPartTypeTable AS C
  44.                 ON A.PartType = C.PartType
  45.             WHERE
  46.                 A.PartID > 1
  47.                 AND
  48.                 C.AddUnsorted = 1
  49.             GROUP BY
  50.                 A.PartID, EntryDate
  51.         ) AS C
  52.         ON
  53.         A.EntryDate = C.EntryDate
  54.         INNER JOIN
  55.         PartTable AS D
  56.         ON B.PartID = D.PartID OR C.PartID = D.PartID
  57.         WHERE
  58.             B.UsedPartCount > 0
  59.             OR
  60.             C.UnsortedPartCount > 0
  61.         GROUP BY
  62.             D.PartID, D.PartType, A.EntryDate
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement