Union和join子句返回重复值

时间:2013-11-13 04:56:45

标签: mysql sql

我的代码有问题,它返回重复值。我试图调整代码,但仍然没有返回我想要的结果。我必须提供五种方案。需要你们的建议或提示。

@BatchID int
--date smalldatetime

AS  

--DP to LN

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
and
a.to_acct_no*=f.acct_no
and
a.batch_id= @BatchID

union all

- GL TO GL

select 
a.batch_id,
a.effective_dt,
a.from_acct_no,
g.description as 'Fromacc',
b.iso_code as 'from_currency',
a.from_crncy,
a.to_crncy,
a.to_acct_no,
h.description as 'Toacc',
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,
gl_acct g,
gl_acct h

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*=g.acct_no
and
a.to_acct_no*=h.acct_no 
and
a.batch_id= @BatchID

union all

--- GL到DP

select 
a.batch_id,
a.effective_dt,
a.from_acct_no,
g.description as 'Fromacc',
b.iso_code as 'from_currency',
a.from_crncy,
a.to_crncy,
a.to_acct_no,
h.title_1 as 'Toacc',
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,
gl_acct g,
dp_acct h

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*=g.acct_no
and
a.to_acct_no*=h.acct_no
and
a.batch_id= @BatchID

union all

- DP到GL

select 
a.batch_id,
a.effective_dt,
a.from_acct_no,
g.title_1 as 'Fromacc',
b.iso_code as 'from_currency',
a.from_crncy,
a.to_crncy,
a.to_acct_no,
h.description as 'Toacc',
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 g,
gl_acct h

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*=g.acct_no
and
a.to_acct_no*=h.acct_no 
and
a.batch_id= @BatchID

union all

- GL到LN

select 
a.batch_id,
a.effective_dt,
a.from_acct_no,
g.description as 'Fromacc',
b.iso_code as 'from_currency',
a.from_crncy,
a.to_crncy,
a.to_acct_no,
h.title_1 as 'Toacc',
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,
gl_acct g,
ln_acct h

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*=g.acct_no
and
a.to_acct_no*=h.acct_no
and
a.batch_id= @BatchID

1 个答案:

答案 0 :(得分:0)

如果您不想要重复,请使用'union'而不是'union all'。

从您的查询结构来看,您的表格设计存在严重问题。