Quantcast
Channel: SQL Server Express forum
Viewing all articles
Browse latest Browse all 3319

Recursive calculation across tables

$
0
0

Hi there,

I try to calculate the account balances of the AdventureWorksDW table FactFinance. The corresponding DimAccount table is recursive.

AccountKey ParentAccountKey AccountDescriptionEN
47 NULL Net Income
48 47 Operating Profit
49 48 Gross Margin
50 49 Net Sales
51 50 Gross Sales
52 51 Intercompany Sales
53 50 Returns and Adjustments
54 50 Discounts
...

Each row in FactFinance refers to the leaf in the account hierarchy. Structure is: AccountKey | Amount

Following cte provides the hierarchy:

WITH  cte (AccountKey, ParentAccountKey)
        AS (
            SELECT  AccountKey, ParentAccountKey
            FROM    DimAccount
            WHERE   DimAccount.AccountKey = 47
            UNION ALL

            SELECT  r.AccountKey, r.ParentAccountKey
            FROM    DimAccount  AS r
                    JOIN cte AS rp
                      ON r.ParentAccountKey = rp.AccountKey	
           )

Select * FROM cte

What I want to do is to provide the account balance for each account (leafes and nodes). Any ideas on how to get all account balances calculated across the hierarchy?

Thanks in advance
Cheers
Hagen

PS: please note that I am only using SQL Sever Express, no Analysis Services available


Viewing all articles
Browse latest Browse all 3319

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>