会计计算 SQL(ssms) 中的借方贷方

Accounting Calculate Debit credit in SQL(ssms)

我有一个会计计算问题。我想用 SQL 查询(在 ssms 中)来编写它。 我有两组文件与一个人(债权人和债务人)有关 债权人文件涵盖债务人文件。 考虑以下示例:(如何实现结果?)

USE [master]
GO

DROP TABLE IF EXISTS #credit/*creditor=0*/,#debit/*Debtor=1*/

SELECT *
INTO #debit
FROM    (values 
        (88,'2/14',1,5,1),(88,'2/15',2,5,1)
        )A (personID,DocDate,DocID,Fee,IsDebit)

SELECT *
INTO #credit
FROM    (values 
        (88,'2/16',3,3,0),(88,'2/17',4,7,0)
        )A (personID,DocDate,DocID,Fee,ISDeb)
SELECT * FROM #credit
SELECT * FROM #debit


--result:
;WITH res AS
(
    SELECT 88 AS personID ,1 deb_DocID ,5 deb_Fee , 3 Cre_DocID ,3 Cre_Fee, 0 remain_Cre_Fee
    UNION
    SELECT 88 AS personID ,1 deb_DocID ,5 deb_Fee , 4 Cre_DocID ,7 Cre_Fee, 5 remain_Cre_Fee
    UNION
    SELECT 88 AS personID ,2 deb_DocID ,5 deb_Fee , 4 Cre_DocID ,7 Cre_Fee, 0 remain_Cre_Fee
)


SELECT *
FROM res

示例数据

使用 ISO 日期格式以避免混淆。

解决方案中不会使用 docdateisdebit 列...

  1. 我忽略了假设下的docdate,即价值是递增的,并且允许在任何借记费用之前存入贷方费用。
  2. 如果您打算在单独的 table 中存储借方和贷方交易,则 isdebit 标志似乎是多余的。

更新样本数据:

create table debit
(
  personid int,
  docdate date,
  docid int,
  fee int,
  isdebit bit
);

insert into debit (personid, docdate, docid, fee, isdebit) values
(88, '2021-02-14', 1, 5, 1),
(88, '2021-02-15', 2, 5, 1);

create table credit
(
  personid int,
  docdate date,
  docid int,
  fee int,
  isdebit bit
);

insert into credit (personid, docdate, docid, fee, isdebit) values
(88, '2021-02-16', 3, 3, 0),
(88, '2021-02-17', 4, 7, 0);

解决方案

这里有几个步骤:

  1. 构建借记费用的滚动总和。完成了第一个常见的 table 表达式 (cte_debit).
  2. 构建学分费用的滚动总和。完成第二个常见的 table 表达式 (cte_credit).
  3. 获取所有借记信息 (select * from cte_debit)
  4. 找到适用于当前借记信息的第一个信用信息。完成第一个 cross apply (cc1)。这包含适用于借方凭证的第一个凭证的 docid
  5. 查找适用于当前借记信息的last 信用信息。完成第二个 cross apply (cc2)。这包含适用于借方凭证的最后一个凭证的 docid
  6. 通过选择第一个和最后一个适用文件 (join cte_credit cc on cc.docid >= cc1.docid and cc.docid <= cc2.docid) 之间的所有文件,找到适用于当前借记信息的 所有 个信用信息。
  7. 合并滚动总和数以计算剩余学分费用 (cc.credit_sum - cd.debit_sum)。使用 case 表达式过滤掉负值。

完整解决方案:

with cte_debit as
(
  select d.personid,
         d.docid,
         d.fee,
         sum(d.fee) over(order by d.docid rows between unbounded preceding and current row) as debit_sum
  from debit d
),
cte_credit as
(
  select c.personid,
         c.docid,
         c.fee,
         sum(c.fee) over(order by c.docid rows between unbounded preceding and current row) as credit_sum
  from credit c
)
select cd.personid,
       cd.docid as deb_docid,
       cd.fee as deb_fee,
       cc.docid as cre_docid,
       cc.fee as cre_fee,
       case
         when cc.credit_sum - cd.debit_sum >= 0
         then cc.credit_sum - cd.debit_sum
         else 0
       end as cre_fee_remaining
from cte_debit cd
cross apply ( select top 1 cc1.docid, cc1.credit_sum
              from cte_credit cc1
              where cc1.personid = cd.personid
                and cc1.credit_sum <= cd.debit_sum
              order by cc1.credit_sum desc ) cc1
cross apply ( select top 1 cc2.docid, cc2.credit_sum
              from cte_credit cc2
              where cc2.personid = cd.personid
                and cc2.credit_sum >= cd.debit_sum
              order by cc2.credit_sum desc ) cc2
join cte_credit cc
  on  cc.personid = cd.personid
  and cc.docid >= cc1.docid
  and cc.docid <= cc2.docid
order by cd.personid,
         cd.docid,
         cc.docid;

结果

personid  deb_docid  deb_fee  cre_docid  cre_fee  cre_fee_remaining
--------  ---------  -------  ---------  -------  -----------------
88        1          5        3          3        0
88        1          5        4          7        5
88        2          5        4          7        0

Fiddle 查看实际情况。这还包含中间 CTE 结果和一些可以取消注释以帮助进一步理解解决方案的注释帮助列。