我的执行查询是:
SELECT rrr.extern_OD_id,
rrr.refund_it_amount,
rrr.refund_amount,
rrr.invoice_ref_7,
rrr.invoice_total_amount
FROM
(SELECT return.extern_OD_id,
return.refund_it_amt AS refund_it_Amount,
return.refund_amount,
billing_tbl.invoice_ref_7,
billing_tbl.invoice_total_amount
FROM
(SELECT rrrf.extern_OD_id,
sum(rrrf.refund_it_amt) AS refund_it_amt,
sum(rrrf.refund_amount/100) refund_amount
FROM rrr__refunds_fact rrrf
WHERE rrrf.refund_status = 'completed'
AND rrrf.refund_created_date_key >= '20150401'
AND rrrf.refund_mode IN('CREDIT_CARD',
'CREDIT_EMI',
'DDCHEQUE',
'DEBIT_CARD',
'GIFT_VOUCHER',
'ICICI',
'NETBANKING',
'back_to_source')
AND rrrf.refund_mode NOT IN ('GIFT_Card')
GROUP BY rrrf.extern_OD_id) RETURN
LEFT JOIN
(SELECT invoice_ref_7,
sum(invoice_total_amount) invoice_total_amount
FROM invoice_fact
WHERE invoice_type_key = 'receiv_note'
AND invoice_status_key NOT IN('voided',
'canceled',
'cancelled')
GROUP BY invoice_ref_7) billing_tbl ON RETURN.extern_OD_id =billing_tbl.invoice_ref_7
WHERE RETURN.refund_amount <> billing_tbl.invoice_total_amount
OR billing_tbl.invoice_ref_7 IS NULL) rrr
WHERE rrr.refund_it_amount >0
AND rrr.refund_amount >0
但是当我尝试加入另一个表并排除一些参数时,它给了我比表srrr_refunds_fact中的记录更多的记录。 它提供了来自新连接表的许多记录,即在rrr__refunds_fact中不存在的Payment_TBL。 你能告诉我哪里出错了。 根据我的理解,如果第一个查询返回n个记录,则在加入新记录之后应该是n或者&lt; Ñ
SELECT rrr.extern_OD_id,
rrr.refund_it_amount,
rrr.refund_amount,
rrr.invoice_ref_7,
rrr.invoice_total_amount
FROM
(SELECT return.extern_OD_id,
return.refund_it_amt AS refund_it_Amount,
return.refund_amount,
billing_tbl.invoice_ref_7,
billing_tbl.invoice_total_amount
FROM
(SELECT rrrf.extern_OD_id,
sum(rrrf.refund_it_amt) AS refund_it_amt,
sum(rrrf.refund_amount/100) refund_amount
FROM rrr__refunds_fact rrrf
JOIN Payment_TBL paymt ON paymt.payment_ref_num_2 = rrrf.extern_OD_id
WHERE rrrf.refund_status = 'completed'
AND rrrf.refund_created_date_key >= '20150401'
AND rrrf.refund_mode IN('CREDIT_CARD',
'CREDIT_EMI',
'DDCHEQUE',
'DEBIT_CARD',
'GIFT_VOUCHER',
'ICICI',
'NETBANKING',
'back_to_source')
AND rrrf.refund_mode NOT IN ('GIFT_Card')
AND paymt.payment_method_key NOT IN ('WALLET')
AND paymt.payment_ref_num_4 NOT IN ('PRICE_REFUND')
GROUP BY rrrf.extern_OD_id) RETURN
LEFT JOIN
(SELECT invoice_ref_7,
sum(invoice_total_amount) invoice_total_amount
FROM invoice_fact
WHERE invoice_type_key = 'receiv_note'
AND invoice_status_key NOT IN('voided',
'canceled',
'cancelled')
GROUP BY invoice_ref_7) billing_tbl ON RETURN.extern_OD_id =billing_tbl.invoice_ref_7
WHERE RETURN.refund_amount <> billing_tbl.invoice_total_amount
OR billing_tbl.invoice_ref_7 IS NULL) rrr
WHERE rrr.refund_it_amount >0
AND rrr.refund_amount >0