结果是12178358.83
SELECT sum(amount) FROM
(
SELECT b.branchabbr,b.category,b.customerno,b.customername,
a.amount amount,a.grossprofit,a.Profitability,
b.accountreceivable,b.xsxde,b.xdqxx,b.owemoneydays,b.validreceivable,b.one,b.two,b.three,b.four,b.five,b.six
FROM t_extendedaccount b
LEFT JOIN dw_xshz_sum a ON b.branchflag=a.fdbs AND b.customerid LIKE a.dwbh||'%'--a.rq LIKE v_month||'%'
WHERE b.statisticsdate='2009-07-06' AND b.branchflag=rpad('fdj',11,' ')
ORDER BY Profitability
)
WHERE amount<>0 OR accountreceivable>0;
结果12148145.32
差了12178358.83-12148145.32,=30213.51
说明结果有错误,然后排错:错误肯定是相关联时,丢数据了,要找到是在哪一步上面丢了数据.
先想用游标,但是感觉有点麻烦.
后来直接用查询来试试:
select sum(amount) from dw_xshz_sum a where not exists(
SELECT 1 FROM t_extendedaccount b
WHERE b.statisticsdate='2009-07-06' AND b.branchflag=rpad('fdj',11,' ')
and b.branchflag=a.fdbs AND b.customerid LIKE a.dwbh||'%'
);
结果是:30213.51 问题就在这里了,t_extendedaccount 表里数据不全.
找到t_extendedaccount 表里差的数据:
select * from (
select "fdbs" fdbs,"dwbh" dwbh from dw_xshz@sql178link where "rq"='2009-07-06' group by "fdbs","dwbh") a
where not exists (select "dwbh" from dw_dwzl@sql178link b where a.fdbs=b."fdbs" and a.dwbh=b."dwbh")