Been working on this report query for quite some time and hitting a brick wall at every turn. No matter which way I approach it the figures suddenly go coompletley out, so I’m hoping that someone can help. I have two tables - delay and delayHistory. I started off creating a query against the delay table which works great. giving me the results I was looking for
SELECT
delayID, delayBookClientRef, MONTH(delayCreated)-1 AS mnth,
COUNT(CASE WHEN delayDecision >=0 OR delayDecision IS NULL THEN delayID end) AS toteD,
COUNT(CASE WHEN delayDecision >=1 THEN delayID end) AS cfrmd,
COUNT(CASE WHEN delayDecision IS NULL THEN delayID end) AS ucfrmd,
COUNT(CASE WHEN delayDecision = 0 THEN delayID end) AS eie,
SUM(
IF(DATEDIFF(delay.delayStopped, delay.delayCreated) IS NULL,
DATEDIFF(CURDATE(),delayCreated),
IF(DATEDIFF(delay.delayBookADD,delay.delayBookEDD) > DATEDIFF(delay.delayStopped, delay.delayCreated)
OR DATEDIFF(delay.delayBookADD,delay.delayBookEDD) IS NULL,
DATEDIFF(delay.delayStopped, delay.delayCreated),DATEDIFF(delay.delayBookADD,delay.delayBookEDD))))
AS maxD,
SUM(
IF(DATEDIFF(delay.delayStopped, delay.delayCreated) IS NULL,
DATEDIFF(CURDATE(),delayCreated),
IF(DATEDIFF(delay.delayBookADD,delay.delayBookEDD) > DATEDIFF(delay.delayStopped, delay.delayCreated)
OR DATEDIFF(delay.delayBookADD,delay.delayBookEDD) IS NULL,
DATEDIFF(delay.delayStopped, delay.delayCreated),DATEDIFF(delay.delayBookADD,delay.delayBookEDD)))) * 120
AS maxC
FROM delay
GROUP BY MONTH(delayCreated)
What I’m trying to do now is get a SUM of the accumulating delay days and cost figures for each delay in the delayHistory table and this is where it all goes horribly wrong. None of the column names are the same in either table and yet by adding this simple line
JOIN delayhistory ON delayhistory.delayIDH = delay.delayID
between the FROM and the GROUP throws all my previous figures out and I’m failing to understand why. I feel as though I’m disappearing up my own query…
Clearly I’m missing something and its probably something really obvious.! or am I going about this in the wrong way? Clearly I’m not a SQL guy but If anyone can help ( in easy to understand words) it would be appreciated.