2013-10-31 161 views
2

我有一个父子关系(层次模型)的帐户图表表。 该表包含两种类型的账户控制账户和交易账户。在各自的父记录中选择子记录的总数

交易账户有余额,但没有任何子账户。

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


 
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 

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

任何帮助将不胜感激

+0

您需要显示您正在从中获取数据的其他表的结构以及如何计算适当的值。 – Harrison

+0

这种类型的查询面临的挑战之一是它是递归的,一般来说SQL并不擅长。但是,由于您在SQL Server中运行,请查看CTE(通用表格表达式)。 – asantaballa

回答

1

方法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 

以上两种方法返回期望的结果。

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 
+0

我的问题中的第一个表是一个包含数据的数据库表,第二个表是我需要在select语句或其他选择的帮助下从该表得到的结果集。不要更新数据库中的数据。 –