在各自的父记录中选择子记录的总数

时间:2013-10-31 17:43:22

标签: sql sql-server select hierarchy

我有一个包含父子关系(层次模型)的帐户表。 该表包含两种类型的帐户控制帐户和交易帐户。

交易账户在没有任何子账户的情况下有余额。

控制帐户没有自己的余额,但是它们带有子帐户。


AccountID|     Title      |AccountType|ParentID| Balance
---------|----------------|-----------|--------|---------
 1       | Assets         |     c     |  null  | null 
 1-1     | Current Assets |     c     |  1     | null
 1-1-1   | Cash           |     t     |  1-1   | 1000
 1-1-2   | Inventory      |     t     |  1-1   | 2000
 1-2     | Fixed Assets   |     c     |  1     | null
 1-2-1   | Furniture      |     t     |  1-2   | 1500
 1-2-2   | Building       |     t     |  1-2   | 3000

我需要一个结果集,如:


AccountID|     Title      |AccountType|ParentID| Balance
---------|----------------|-----------|--------|---------
 1       | Assets         |     c     |  null  | 7500 --sum of current and fixed Assets
 1-1     | Current Assets |     c     |  1     | 3000 --sum of cash and inventory
 1-1-1   | Cash           |     t     |  1-1   | 1000
 1-1-2   | Inventory      |     t     |  1-1   | 2000
 1-2     | Fixed Assets   |     c     |  1     | 4500 --sum of furniture and building
 1-2-1   | Furniture      |     t     |  1-2   | 1500
 1-2-2   | Building       |     t     |  1-2   | 3000

交易表


ID |AccountID|Amount
---|---------|------
 1 | 1-1-1   |  300  
 2 | 1-1-1   |  700
 3 | 1-1-2   | 1500
 4 | 1-1-2   |  500
 5 | 1-2-1   |  700
 6 | 1-2-1   |  800
 7 | 1-2-2   | 2000
 8 | 1-2-2   | 1000

如果可能,任何选择语句 或功能或存储过程。

任何帮助将不胜感激

2 个答案:

答案 0 :(得分:2)

方法1(感谢asantaballa将我的注意力转向CTE)

With TrialBalance(AccountID, ParentID, Balance)
AS
(
Select AccountId, ParentID, Balance From Accounts Where AccountType = 't'
Union All
Select A.AccountId, A.ParentID, T.Balance From Accounts A
Inner Join TrialBalance T On A.AccountId = T.ParentID
)
Select AccountID, SUM(Balance) From TrialBalance Group By AccountID    

方法2

Create Function AccountBalance(@AccountID Nvarchar(100))
Returns Float
AS
Begin
Declare @Balance Float
Select @Balance = Balance From Accounts Where AccountID = @AccountID
Select @Balance += Sum(dbo.AccountBalance(AccountID)) From Accounts
Where ParentID = @AccountID
Return @Balance
End

Select AccountID, dbo.AccountBalance(AccountID) From Accounts

以上两种方法都会返回所需的结果。

答案 1 :(得分:0)

有很多方法可以做到这一点,但是我使用了几个更新语句。 请注意,您还没有提供任何数据库结构,因此我认为您有2个表 - 一个包含帐户,另一个包含交易帐户的余额:

create table #accounts (
AccountId int
,Title varchar(60)
,AccountType varchar(10)
,ParentID int
,Balance int
)
create table #transactional_accounts (
Id int
,AccountID int
,Amount int
)

insert into #accounts (AccountId, Title, AccountType,ParentID) values
..............
insert into #transactional_accounts values
........... 

 update #accounts
 set Balance= (select SUM(amount) from #transactional_accounts t2 where t1.AccountId= t2.AccountID  )
from #accounts t1
where AccountType ='t'


update #accounts
set Balance= case when t1.ParentID is not null then (select SUM(t2.Balance) from #accounts t2 where t1.AccountId=t2.ParentID)
          else (select SUM(t2.Balance) from #accounts t2)
          end
from #accounts t1
where AccountType ='c'

select * from #accounts