簡體   English   中英

如何使用借記卡獲得未結余額

[英]How to get the Outstanding balance using debit credit

我想使用借記貸記獲得所有承包商和供應商的未結余額,但問題是有多個文件編號,因此我無法將它們與借記貸記相關聯以成為余額。

我嘗試關聯 cdvno 但問題是有多個條目所以有可能將它們相互關聯

SELECT b.amount,a.cdvno, a.debit, a.credit, a.Debit + a.Credit - b.amount 'balance'
    FROM cdvdtl a left join
         cdvhdr b
         on b.cdvno = a.cdvno and b.trantype = a.trantype

如果可能的話,我希望結果是這樣的,只有 1 個 cdvno output

amount          cdvno           debit      credit      balance
15000.00    000-2016-02000009   0.00       15000.00 0.00
15000.00    000-2016-02000009   15000.00    0.00    0.00

但結果是這樣的

amount  cdvno              debit    credit  balance
596.64  000-2016-01000617   0.00    596.64  0.00
596.64  000-2016-01000617   0.00    6.03    -590.61
596.64  000-2016-01000617   602.67  0.00    6.03

我的表中的示例數據 1 cdvdtl cdvhdr 借方貸方

我不明白 [amount] 列背后的邏輯,因為它似乎沒有遵循借方或貸方的數學運算。

但是,一種解決您分散借方和貸方情況的方法可能是使用 group by

    select amount, cdvno, sum(debit) as [debit], sum(credit) as [credit],sum(balance) as [balance] from [tablename] group by cdvno

如果我理解正確,這是一種使用union all來分隔記錄並獲得每個cdvno 2 行的解決方案,其中包含debit余額和credit余額。

select a.cdvno, a.debit, a.credit, a.credit + a.debit - coalesce(b.bamount,0)
from (
    select cdvno, debit, sum(credit) credit, trantype
    from cdvdtl  
    where debit = 0
    group by cdvno, debit, trantype
    union all
    select cdvno, sum(debit) debit, credit, trantype
    from cdvdtl  
    where credit = 0
    group by cdvno, credit, trantype
) a left join cdvhdr b
     on b.cdvno = a.cdvno and b.trantype = a.trantype

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM