I have a query where I'm looking for the Top 5 results of downtime over a 4-week period in our database, and getting the information from each of those 5 categories for each of those 4 weeks. The problem is that some of those weeks may not have any results for one of those 5 categories. How do I make the query return a result where it includes the parameter that had no result and then the quantity for the correlating column be 0?
And before you ask, the reason I need it this way is because it's going in a stacked bar chart, and without the 0 value, it will create gaps between the stacked bars.
SELECT dispatchl2ldb.dispatchType,
dispatchl2ldb.reasonDesc,
IF(dispatchl2ldb.reasonDesc = :topCat1,1,
IF(dispatchl2ldb.reasonDesc = :topCat2,2,
IF(dispatchl2ldb.reasonDesc = :topCat3,3,
IF(dispatchl2ldb.reasonDesc = :topCat4,4,
IF(dispatchl2ldb.reasonDesc = :topCat5,5,0))))) AS catOrder,
SUM(dispatchl2ldb.minsDown) AS minTotal,
WEEKOFYEAR(dispatchl2ldb.reported) AS Week
FROM dispatchl2ldb
WHERE dispatchl2ldb.reasonDesc = :topCat1
AND dispatchl2ldb.line = :line
AND dispatchl2ldb.shiftID = :shiftID
AND dispatchl2ldb.dispatchType = "Waiting On"
AND WEEKOFYEAR(dispatchl2ldb.reported) = :week
OR dispatchl2ldb.reasonDesc = :topCat2
AND dispatchl2ldb.line = :line
AND dispatchl2ldb.shiftID = :shiftID
AND dispatchl2ldb.dispatchType = "Waiting On"
AND WEEKOFYEAR(dispatchl2ldb.reported) = :week
OR dispatchl2ldb.reasonDesc = :topCat3
AND dispatchl2ldb.line = :line
AND dispatchl2ldb.shiftID = :shiftID
AND dispatchl2ldb.dispatchType = "Waiting On"
AND WEEKOFYEAR(dispatchl2ldb.reported) = :week
OR dispatchl2ldb.reasonDesc = :topCat4
AND dispatchl2ldb.line = :line
AND dispatchl2ldb.shiftID = :shiftID
AND dispatchl2ldb.dispatchType = "Waiting On"
AND WEEKOFYEAR(dispatchl2ldb.reported) = :week
OR dispatchl2ldb.reasonDesc = :topCat5
AND dispatchl2ldb.line = :line
AND dispatchl2ldb.shiftID = :shiftID
AND dispatchl2ldb.dispatchType = "Waiting On"
AND WEEKOFYEAR(dispatchl2ldb.reported) = :week
OR dispatchl2ldb.reasonDesc = :topCat1
AND dispatchl2ldb.line = :line
AND dispatchl2ldb.shiftID = :shiftID
AND dispatchl2ldb.dispatchType = "Operational Fix"
AND WEEKOFYEAR(dispatchl2ldb.reported) = :week
OR dispatchl2ldb.reasonDesc = :topCat2
AND dispatchl2ldb.line = :line
AND dispatchl2ldb.shiftID = :shiftID
AND dispatchl2ldb.dispatchType = "Operational Fix"
AND WEEKOFYEAR(dispatchl2ldb.reported) = :week
OR dispatchl2ldb.reasonDesc = :topCat3
AND dispatchl2ldb.line = :line
AND dispatchl2ldb.shiftID = :shiftID
AND dispatchl2ldb.dispatchType = "Operational Fix"
AND WEEKOFYEAR(dispatchl2ldb.reported) = :week
OR dispatchl2ldb.reasonDesc = :topCat4
AND dispatchl2ldb.line = :line
AND dispatchl2ldb.shiftID = :shiftID
AND dispatchl2ldb.dispatchType = "Operational Fix"
AND WEEKOFYEAR(dispatchl2ldb.reported) = :week
OR dispatchl2ldb.reasonDesc = :topCat5
AND dispatchl2ldb.line = :line
AND dispatchl2ldb.shiftID = :shiftID
AND dispatchl2ldb.dispatchType = "Operational Fix"
AND WEEKOFYEAR(dispatchl2ldb.reported) = :week
OR dispatchl2ldb.reasonDesc = :topCat1
AND dispatchl2ldb.line = :line
AND dispatchl2ldb.shiftID = :shiftID
AND dispatchl2ldb.dispatchType = "FDT"
AND WEEKOFYEAR(dispatchl2ldb.reported) = :week
OR dispatchl2ldb.reasonDesc = :topCat2
AND dispatchl2ldb.line = :line
AND dispatchl2ldb.shiftID = :shiftID
AND dispatchl2ldb.dispatchType = "FDT"
AND WEEKOFYEAR(dispatchl2ldb.reported) = :week
OR dispatchl2ldb.reasonDesc = :topCat3
AND dispatchl2ldb.line = :line
AND dispatchl2ldb.shiftID = :shiftID
AND dispatchl2ldb.dispatchType = "FDT"
AND WEEKOFYEAR(dispatchl2ldb.reported) = :week
OR dispatchl2ldb.reasonDesc = :topCat4
AND dispatchl2ldb.line = :line
AND dispatchl2ldb.shiftID = :shiftID
AND dispatchl2ldb.dispatchType = "FDT"
AND WEEKOFYEAR(dispatchl2ldb.reported) = :week
OR dispatchl2ldb.reasonDesc = :topCat5
AND dispatchl2ldb.line = :line
AND dispatchl2ldb.shiftID = :shiftID
AND dispatchl2ldb.dispatchType = "FDT"
AND WEEKOFYEAR(dispatchl2ldb.reported) = :week
GROUP BY reasonDesc, Week
ORDER BY catOrder ASC