我尽力简化我的问题。用户需要根据 batch_id 从数据库中进行选择。(有多种交易方式)
使用下面的查询它不会返回所有的事务。应该返回116行但它只返回115行。因为 to_acct_no 列值无效而导致一行丢失的原因。这意味着何时如果 from_acct_no 或 to_acct_no 无效,我的查询不包含条件,则会发生交易。 from_acct_no 或 to_acct_no 无效。需要建议使用哪种方法。
使用NULL但仍然返回相同的结果,因为列中有值但没有正确的值。
Table
Batch_id | From_acct_no | to_acct_no
61619 | 001102125698 | 01278965321
61619 | 001103568965 | 013052542
查询
SELECT a.batch_id,
a.effective_dt,
a.from_acct_no,
e.title_1,
b.iso_code AS 'from_currency',
a.from_crncy,
a.to_crncy,
a.to_acct_no,
f.title_1,
c.iso_code AS 'to_currency',
a.to_posted_amt,
a.status,
a.rej_reason,
d.short_text,
a.from_description,
a.from_amt,
a.batch_tran_id,
d.error_text
FROM gb_batch_tfr_trans a,
ad_gb_crncy b,
ad_gb_crncy c,
pc_ov_error d,
dp_acct e,
ln_acct f
WHERE a.from_crncy = b.crncy_id
AND a.to_crncy = c.crncy_id
AND d.error_id =* a.rej_reason
AND a.status IN ( 'rejected' )
AND ( a.from_acct_no = e.acct_no
OR a.from_acct_no IS NULL
AND e.acct_no IS NULL )
AND ( a.to_acct_no = f.acct_no
OR a.to_acct_no IS NULL
AND f.acct_no IS NULL )
AND a.batch_id = 61619
添加了代码(我遵循基于给定答案的代码结构)
union
SELECT a.batch_id,
a.effective_dt,
a.from_acct_no,
e.description,
b.iso_code AS 'from_currency',
a.from_crncy,
a.to_crncy,
a.to_acct_no,
f.description,
c.iso_code AS 'to_currency',
a.to_posted_amt,
a.status,
a.rej_reason,
d.short_text,
a.from_description,
a.from_amt,
a.batch_tran_id,
d.error_text
FROM gb_batch_tfr_trans a
INNER JOIN ad_gb_crncy b ON a.from_crncy = b.crncy_id
INNER JOIN ad_gb_crncy c ON a.to_crncy = c.crncy_id
LEFT OUTER JOIN pc_ov_error d ON a.rej_reason = d.error_id
LEFT OUTER JOIN gl_acct e ON a.from_acct_no = e.acct_no
LEFT OUTER JOIN gl_acct f ON a.to_acct_no = f.acct_no
WHERE a.status IN ( 'rejected' )
AND a.batch_id = 61619
答案 0 :(得分:0)
我认为问题可能是您正在使用表“dp_acct”和“ln_acct”的内部联接。 尝试:
SELECT a.batch_id,
a.effective_dt,
a.from_acct_no,
e.title_1,
b.iso_code AS 'from_currency',
a.from_crncy,
a.to_crncy,
a.to_acct_no,
f.title_1,
c.iso_code AS 'to_currency',
a.to_posted_amt,
a.status,
a.rej_reason,
d.short_text,
a.from_description,
a.from_amt,
a.batch_tran_id,
d.error_text
FROM gb_batch_tfr_trans a
INNER JOIN ad_gb_crncy b ON a.from_crncy = b.crncy_id
INNER JOIN ad_gb_crncy c ON a.to_crncy = c.crncy_id
LEFT OUTER JOIN pc_ov_error d ON a.rej_reason = d.error_id
LEFT OUTER JOIN dp_acct e ON a.from_acct_no = e.acct_no
LEFT OUTER JOIN ln_acct f ON a.to_acct_no = f.acct_no
WHERE a.status IN ( 'rejected' )
AND a.batch_id = 61619