I've been trying to get a query/subquery to work for some stats but have failed, so I started again from scratch. I can now get the results I want but it's still in two separate queries. I have little to no experience with sub queries but after reasearching, my heart tells me that I should be able to do this in one query. All the info is in one table but I need to use two separate GROUP BY in order to get the right data. I'm hoping that someone can help me get my head around this or at least point me in the right direction... Thanks in advance
SELECT MONTH(bookADhistory)-1 AS monthNum,
COUNT(DISTINCT bookIDHistory) AS totalBooks,
COUNT(DISTINCT CASE WHEN bookDDCHistory = 300 THEN bookIDHistory end) AS breaches
FROM bookhistory
JOIN book ON bookID = bookIDHistory
WHERE bookID = 44
GROUP BY MONTH (bookADhistory) ORDER BY monthNum;
SELECT MONTH(historyCreated)-1 AS monthNum,
COUNT(DISTINCT CASE WHEN bookDDCHistory BETWEEN 1 AND 99 THEN bookIDHistory end) AS delays,
COUNT(DISTINCT CASE WHEN bookDDCHistory = 200 THEN bookIDHistory end) AS extns,
COUNT(DISTINCT CASE WHEN bookDDCHistory = 100 THEN bookIDHistory end) AS lateClose
FROM bookhistory
JOIN book ON bookID = bookIDHistory
WHERE bookID = 44
GROUP BY MONTH (historyCreated) ORDER BY monthNum;