用最快的算法计算余额 sheet
Calculate balance sheet with fastest algorithm
我正在实施会计软件。
在计算分层自引用题目的平衡sheet时,请告诉我最快的算法
这些是我的 tables:
主题 table:
TopicID nvarchar(50) -- is Parent Field
ParentID nvarchar(50) -- is Child Field
Description nvarchar(512)
------------DocumentDetal table
DocumentNumber nvarchar(50)
TopicFK nvarchar(50)
Debit decimal(18,0)
Credit decimal(18,0)
两个 table 与 TopicID
和 TopicFK
列相关,请告诉我如何使用存储的 SQL 计算余额 sheet程序。
以下是数据样本:
以下是文档:
其实我想要以下计算结果:
为了你的SQL Server 2008 R2
,这是为了sumDebit and sumCredit
。不明白如何计算 Res Debit and Res credit
,但我认为您也可以编辑以获得 Res value
。
无论如何,这是使用 CTE
感谢 Mikael Eriksson
在 Recursive sum in tree structure
with T as
(
select t.TopicID, t.ParentID, sum(d.Debit) as sumDebit, sum(d.Credit) as sumCredit
from Topics t
left join DocumentDetail d
on t.TopicID = d.TopicFK
group by t.TopicID, t.ParentID
)
,C as
(
select T.TopicID,
T.sumDebit,
T.sumCredit,
T.TopicID as RootID
from T
union all
select T.TopicID,
T.sumDebit,
T.sumCredit,
C.RootID
from T
inner join C
on T.ParentId = C.TopicID
)
select T.TopicID,
T.ParentId,
S.sumDebitIncludingChildren sumDebit,
S.sumCreditIncludingChildren sumCredit
from T
inner join (
select RootID,
sum(sumDebit) as sumDebitIncludingChildren,
sum(sumCredit) as sumCreditIncludingChildren
from C
group by RootID
) as S
on T.TopicID = S.RootID
order by T.TopicID
option (maxrecursion 0);
中测试正常
我正在实施会计软件。
在计算分层自引用题目的平衡sheet时,请告诉我最快的算法
这些是我的 tables:
主题 table:
TopicID nvarchar(50) -- is Parent Field
ParentID nvarchar(50) -- is Child Field
Description nvarchar(512)
------------DocumentDetal table
DocumentNumber nvarchar(50)
TopicFK nvarchar(50)
Debit decimal(18,0)
Credit decimal(18,0)
两个 table 与 TopicID
和 TopicFK
列相关,请告诉我如何使用存储的 SQL 计算余额 sheet程序。
以下是数据样本:
以下是文档:
其实我想要以下计算结果:
为了你的SQL Server 2008 R2
,这是为了sumDebit and sumCredit
。不明白如何计算 Res Debit and Res credit
,但我认为您也可以编辑以获得 Res value
。
无论如何,这是使用 CTE
感谢 Mikael Eriksson
在 Recursive sum in tree structure
with T as
(
select t.TopicID, t.ParentID, sum(d.Debit) as sumDebit, sum(d.Credit) as sumCredit
from Topics t
left join DocumentDetail d
on t.TopicID = d.TopicFK
group by t.TopicID, t.ParentID
)
,C as
(
select T.TopicID,
T.sumDebit,
T.sumCredit,
T.TopicID as RootID
from T
union all
select T.TopicID,
T.sumDebit,
T.sumCredit,
C.RootID
from T
inner join C
on T.ParentId = C.TopicID
)
select T.TopicID,
T.ParentId,
S.sumDebitIncludingChildren sumDebit,
S.sumCreditIncludingChildren sumCredit
from T
inner join (
select RootID,
sum(sumDebit) as sumDebitIncludingChildren,
sum(sumCredit) as sumCreditIncludingChildren
from C
group by RootID
) as S
on T.TopicID = S.RootID
order by T.TopicID
option (maxrecursion 0);
中测试正常