0

重複した値を返すコードに問題があります。コードを微調整しようとしましたが、希望する結果が返されませんでした。私が対応しなければならない 5 つのシナリオがあります。皆さんからのアドバイスやヒントが必要です。

@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
4

1 に答える 1

0

重複したくない場合は、「union all」の代わりに「union」を使用してください。

また、クエリの構造から判断すると、テーブルの設計には深刻な問題があります。

于 2013-11-13T06:17:59.447 に答える