-1

我有以下两个表,其中存储有关信用和借记记录的信息。

凭证Cr 表包含

voucherType    voucherPrefix    voucherNo    crparty    cramount
SALES          S                1            1          43000
SALES          S                2            1          10000

凭证Dr 表包含

voucherType    voucherPrefix    voucherNo    drparty    dramount
SALES          S                1            5          43000
SALES          S                2            5          10000

现在在这里,在 SALES 凭证 S/1 中,第 1 方已贷记 43000 金额,第 5 方已获得相同金额。SALES 凭证 S/2 也是如此,其中第 1 方已记入 10000 金额,而第 5 方也有相同金额。

现在我想显示如下结果如果我查询第 1 方

PARTY    CREDIT    DEBIT    DEBITPARTY    voucherType    voucherPrefix    voucherNo
1        43000              5              SALES          S                1
1        10000              5              SALES          S                2

请帮忙

4

2 回答 2

0

如果我正确理解了您的问题,那么这就是您要寻找的

Select c.crParty as Party, d.dramount as credit , null as debit,
d.drParty as DEBITPARTY,c.voucherType as voucherType,
d.voucherPrefix,d.voucherNo
from VoucherCr as c inner join VoucherDr as d
on c.voucherNo=d.VoucherNo and c.voucherPrefix=d.voucherPrefix
where c.crparty=1
group by d.dramount,c.cramount,d.voucherPrefix,d.voucherNo,c.crParty,
c.voucherType,d.drParty
order by d.dramount desc

试试SQLFIDDLE

于 2012-12-25T06:22:47.297 回答
0

尝试使用此查询。在您的情况下,是否有可能将一个dramount分为多行voucherDr?例如 43000->40000+3000

select 
vc.Party,vc.CrAmount, vd.drAmount, vd.drparty, 
vc.voucherType, vc.voucherPrefix, vc.voucherNo

from voucherCr vc
left join voucherDr vd on (vc.voucherType=vd.voucherType)
                          and (vc.voucherPrefix=vd.voucherPrefix)
                          and (vc.voucherNo=vd.voucherNo)

where vc.PARTY=1
于 2012-12-25T05:37:25.420 回答