I need to add two different fields in two separate tables, but the second sum is a multiple of what it should be. Here's what I'm using:
((Sum(If(B.ttc In ('X','Y','Z'), 0, B.ua))) -
(Sum(If(B.ttc In ('X','Y','Z'), B.ua, 0)))) As Total_UA,
(Sum(C.cub)) As Total_CUB
From Table_B As B
Left Join Table_C As C On B.acctno = C.acctno
Left Join Table_A As A On B.acctno = A.acctno
Where A.status != 'C'
Group By B.acctno Having Abs(Total_UA - Total_CUB) > 0.0001
Total_UA is coming out fine, but Total_CUB is double what it should be. Any help would be much appreciated as to what I'm doing wrong or if there is a better way of doing this. It seems like the result of Total_CUB is a multiple of what it should be by the number of rows in the grouping. If my understanding of MySQL was better, I might know how to get around this. This is a critical issue I'm trying to fix. This query is needed to vet important data.