I'm going cross-eyed here... I have a query that is giving me the results I want - the total of all of the categories listed by bill number and month
However, when I try to add a self join to get the subtotal of a certain group of billing catagories, the results are incorrect
All of the original data is correct, but the new column total is inaccurate. For instance if there is a $8000 credit in January 2006, it will be listed as a $32,000 credit. Is it somehow joining 4 times?
Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook
Code:
SELECT
b.i_cbr_no
, b.q_processing_month
, b.t_doc_no
, sum(bt.a_bill)
FROM oaBill.t_oa_billed b
JOIN oaBill.t_bill_tran bt
ON b.i_oa_no = bt.i_oa_no
AND b.q_processing_month = bt.q_processing_month
WHERE b.c_region = '11'
AND b.c_ab_code = '9799'
AND b.q_processing_month > 200509
GROUP BY b.i_cbr_no
, b.q_processing_month
, b.t_doc_no
ORDER BY b.i_cbr_no, b.q_processing_month
Code:
SELECT
b.i_cbr_no
, b.q_processing_month
, b.t_doc_no
, sum(bt.a_bill)
[red], sum(bt2.a_bill)[/red]
FROM oaBill.t_oa_billed b
JOIN oaBill.t_bill_tran bt
ON b.i_oa_no = bt.i_oa_no
AND b.q_processing_month = bt.q_processing_month
[red]LEFT JOIN oaBill.t_bill_tran bt2
ON b.i_oa_no = bt2.i_oa_no
AND b.q_processing_month = bt2.q_processing_month
AND bt2.c_rectype in('190','200','210','220','230')[/red]
WHERE b.c_region = '11'
AND b.c_ab_code = '9799'
AND b.q_processing_month > 200509
GROUP BY b.i_cbr_no
, b.q_processing_month
, b.t_doc_no
ORDER BY b.i_cbr_no, b.q_processing_month
Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook