2012-12-25 53 views
-1

我有以下兩個表,其中存儲有關信用和借記記錄的信息。從多個表中顯示帳戶餘額

voucherCr表包含

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

voucherDr表包含

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

現在這裏,在銷售憑證S/1,甲方1一直與信貸金額43000相同agains量的方5。銷售憑證S/2也是如此,其中第一方已與同等金額的第五方貸記10000金額。

現在我想顯示結果如下:如果我查詢有關方1

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

請幫

+0

[Rü考慮voucherNo沿着匹配與表格憑證Dr的金額? – praveen

回答

0

嘗試使用此查詢。在你的情況下,dramountvoucherDr中被劃分爲多行可能嗎?例如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 
+0

@AndriyM,我不是粗魯的,我需要的答案類似於Praveen –

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