Click here to Skip to main content
15,884,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi...
I have a table (fctTable), which references another table (refTable) that has a parent/child relationship to it's self.

What i need to do is for each row in the fctTable i need to get the sum of a column on the refTable for whole tree of parents.

this is hard to explain so i have an example...

Here are my two tables:

fctTable

IdrefTableId
12
27
34
49
58


refTable

IdParentIdValue
1017
2111
3023
455
537
7213
803
9819


and i need to get this out:
Result

fctTableId(refTableIds)SUM(refTable.Value)
12,111 + 17 = 28
27,2,113 + 11 + 17 = 41
34,5,35 + 7 + 23 = 35
49,819 + 3 = 22
583 = 3


I don't need the refTableId's column in the results, thats just to help me work out what the heck i'm doing....

I can do it by creating a table valued function similar to the SP mentioned by Anish M here, which gets the tree for a given refTableId, and then calling that from a select statment that chooses the rows in the fctTable i want to use.

but that seems clunky and slow (i may have millions of records in the fctTable to do this for so slow ≡ bad)

I was wondering if any one can think of a better solution... any ideas?
Posted

1 solution

Mark replied to with an answer to my post on SQLServerCentral here.

Thanks everyone for taking part!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900