0

可能重复:
显示多个表中的帐户余额

我有两个表如下

凭证Cr

srno    vouchertype voucherprefix   voucherno   crparty cramount
1        PURCHASE       P              1            2   55000
2        PAYMENT        R              1            1   55000

凭证博士

srno    vouchertype voucherprefix   voucherno   drparty dramount
1        PURCHASE       P              1            4   54000
2        PAYMENT        R              1            2   55000
3        PURCHASE       P              1            4       1000 

在这里,在 PURCHASE 凭证 P/1 中,我从第 2 方购买价值 55000 的商品,在 PAYMENT 凭证 R/1 中,我向第 2 方支付 55000 卢比。

现在如果我查询第 2 方,我想以以下格式显示信息

VTYPE      VPRE      VNO      AGAINSTPARTY      CREDIT      DEBIT
PURCHASE   P         1        4                 55000       NULL
PAYMENT    R         1        1                 NULL        55000

因此,第 2 方的期末余额将为零,因为我已向他们支付 55000,而不是购买 55000

你能帮助我吗?

更新 我尝试了以下但没有结果

select * from voucherCr vc
full outer join voucherDr vd
on vc.voucherno=vd.voucherno
and vc.voucherprefix=vd.voucherprefix
and vc.vouchertype=vd.vouchertype
where vc.crparty=2 or vd.drparty=2

更新 2

如果任何一个表中有多个条目用于相同的凭证编号和凭证前缀,我会得到错误的结果。

4

2 回答 2

1

我认为你需要这个:

select C.vouchertype,C.voucherprefix,C.voucherno,D.drparty,C.cramount AS CREDIT,NULL as DEBIT
from voucherCr C
inner join voucherDr D on C.vouchertype=D.vouchertype and C.voucherprefix=D.voucherprefix    and C.voucherno=D.voucherno
and C.crparty=2
union
select D.vouchertype,D.voucherprefix,D.voucherno,C.crparty,NULL as CREDIT,D.dramount AS   DEBIT
from voucherCr C
inner join voucherDr D on C.vouchertype=D.vouchertype and C.voucherprefix=D.voucherprefix and C.voucherno=D.voucherno
and D.Drparty=2
于 2012-12-25T08:17:07.950 回答
0
`SELECT *   
 FROM voucherCr VC  
   FULL OUTER JOIN voucherDr VD ON VC.crparty = VD.drparty  
 WHERE  
   (VC.crparty IS NULL OR VD.drparty IS NULL)`

示例:http ://www.sqlfiddle.com/#!3/74419/45

于 2012-12-25T10:07:54.487 回答