当我将两个表连接在一起并得到总和时,我得到了正确的数字:
select
d.accn_id,
cast(d.load_date as DATE) as LoadDate,
cast (d.final_rpt_date as DATE) as FinalReportDate,
sum(p.paid_amt) as SumPaidAmt
from
accn_demographics d
join
accn_payments p
on
d.ACCN_ID=p.ACCN_ID
where
p.POSTED='y'
and p.PMT_DATE between '20120501' and '20120531'
group by
d.accn_id,
d.load_date,
d.final_rpt_date
但是在我加入另一个表 accn_payors 之后:
select
d.accn_id,
cast(d.load_date as DATE) as LoadDate,
cast (d.final_rpt_date as DATE) as FinalReportDate,
sum(p.paid_amt) as SumPaidAmt
,payors.PAYOR_ID
from
accn_demographics d
join
accn_payments p
on
d.ACCN_ID=p.ACCN_ID
left join
accn_payors payors
on
payors.X_PAYOR_ID=p.X_PRICED_PAYOR_ID
and payors.ACCN_ID = p.ACCN_ID
where
p.POSTED='y'
and p.PMT_DATE between '20120501' and '20120531'
group by
d.accn_id,
d.load_date,
d.final_rpt_date
,payors.PAYOR_ID
我夸大了sum(p.paid_amt)
问题是如何调整我的加入,以免我多次加入?