如何使用借记贷记获得未结余额
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 输出
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 debit credit
我不明白 [金额] 列背后的逻辑,因为它似乎不遵循借方和贷方的数学 运行。
但是解决分散借方和贷方情况的一种方法可能是使用分组依据
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
我想获得所有使用借记贷记的承包商和供应商的未结余额,但问题是有多个单据编号,所以我无法将它们与借记贷记联系起来成为余额。
我尝试关联 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 输出
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 debit credit
我不明白 [金额] 列背后的逻辑,因为它似乎不遵循借方和贷方的数学 运行。
但是解决分散借方和贷方情况的一种方法可能是使用分组依据
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