2013-10-31 81 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語句或其他選擇的幫助下從該表得到的結果集。不要更新數據庫中的數據。 –