Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- SELECT
- D.PartID,
- D.PartType,
- A.EntryDate,
- COALESCE(SUM(B.UsedPartCount),9) AS UsedPartCount,
- COALESCE(SUM(C.UnsortedPartCount),0) AS UnsortedPartCount
- FROM
- DateTable AS A
- LEFT JOIN
- (
- SELECT
- COALESCE(COUNT(A.JobEntryPartID),0) AS UsedPartCount,
- A.PartID,
- DATEADD(dd,0,DATEDIFF(dd,0,B.RepairDate)) AS EntryDate
- FROM
- JobEntryPartTable AS A
- INNER JOIN
- JobEntryTable AS B
- ON A.JobEntryID = B.JobEntryID
- INNER JOIN
- JobPartTypeTable AS C
- ON A.PartType = C.PartType
- WHERE
- A.PartID > 1
- AND
- C.Remove = 1
- GROUP BY
- A.PartID, EntryDate
- ) AS B
- ON A.EntryDate = B.EntryDate
- LEFT JOIN
- (
- SELECT
- COUNT(A.JobEntryPartID) AS UnsortedPartCount,
- A.PartID,
- DATEADD(dd,0,DATEDIFF(dd,0,B.RepairDate)) AS RepairDate
- FROM
- JobEntryPartTable AS A
- INNER JOIN
- JobEntryTable AS B
- ON A.JobEntryID = B.JobEntryID
- INNER JOIN
- JobPartTypeTable AS C
- ON A.PartType = C.PartType
- WHERE
- A.PartID > 1
- AND
- C.AddUnsorted = 1
- GROUP BY
- A.PartID, EntryDate
- ) AS C
- ON
- A.EntryDate = C.EntryDate
- INNER JOIN
- PartTable AS D
- ON B.PartID = D.PartID OR C.PartID = D.PartID
- WHERE
- B.UsedPartCount > 0
- OR
- C.UnsortedPartCount > 0
- GROUP BY
- D.PartID, D.PartType, A.EntryDate
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement