I am trying to calculate totals for parent nodes of a tree structure, and for some reason, calculating the parent node(s) total value is eluding me.
Assume I have three tables
Regions
GroupID ParentID Name
1 null NorthAmerica
2 null Asia
3 null Europe
4 1 NorthEast
5 1 WestCoast
6 3 UK
7 3 Germany
8 2 Hong Kong
9 2 Japan
RegionMember
GroupID EmpID
4 10000
4 10001
5 10011
6 20455
6 10003
7 34567
9 43589
9 54890
8 84320
8 84560
EmployeeSales
EmployeeID Name Sales ($)
10000 Joe $ 150,000.00
10001 Mary $ 200,000.00
10011 John $ 175,000.00
20455 Fred $ 100,000.00
10003 Bill $ 250,000.00
34567 Abe $ 142,000.00
43589 Jack $ 260,000.00
54890 Amanda $ 300,000.00
84320 Jane $ 15,000.00
84560 Oscar $ 175,000.00
The goal is to query for different levels within the tree and see the totals for those regions.
For example, one view would show top regions with sales totals:
NorthAmerica 525,000.00 *(The sum of NorthEast and WestCoast)*
Asia 750,000.00 *(The sum of Hong Kong and Japan)*
Europe 492,000.00 *(The sum of UK and Germany)*
Another view would show regional totals (focused for a single parent):
NorthAmerica 525,000.00 *(Total of the region members NorthEast and WestCoast)*
Northeast 350,000.00 *(Total of NorthEast Leaves Joe and Mary)*
WestCoast 175,000.00 *(Total of WestCoast Leaves John)*
Of course, these trees could go much deeper in terms of branches, but I think the example illustrates the problem I’m fighting.
So far, using CTE’s I can fairly easily navigate the tree structures, and I can get totals for the final branches (or leaves of the branch), but I can’t seem to get the rolled-up totals to come through.
So from the above example, I can get the following output:
NorthAmerica NULL
NorthEast 350,000.00
WestCoast 175,000.00
I would provide the existing code, but the actual tables and the number of joins are much different in my actual table, and might just confuse the overall goal. However, this question is similar to what I am looking to accomplish, but it doesn't seem to totally fit the fill:
Any help is greatly appreciated.
Build Scripts Follow:
create table Regions
(
GroupID int,
ParentID int,
Name Varchar(40)
)
create table RegionMember
(
GroupID int,
empid int
)
Create Table EmployeeSales
(
EmployeeID int,
Name Varchar(50),
Sales float,
)
Insert into Regions Values
(1, null, 'NorthAmerica'),
(2, null, 'Asia'),
(3, null, 'Europe'),
(4, 1, 'NorthEast'),
(5, 1, 'WestCoast'),
(6, 3, 'UK'),
(7, 3, 'Germany'),
(8, 2, 'Hong Kong'),
(9, 2, 'Japan');
Insert into RegionMember Values
(4, 10000),
(4, 10001),
(5, 10011),
(6, 20455),
(6, 10003),
(7, 34567),
(9, 43589),
(9, 54890),
(8, 84320),
(8, 84560);
Insert into EmployeeSales Values
(10000, 'Joe', 150000),
(10001, 'Mary', 200000),
(10011, 'John', 175000),
(20455, 'Fred', 100000),
(10003, 'Bill', 250000),
(34567, 'Abe', 142000),
(43589, 'Jack', 260000),
(54890, 'Amanda', 300000),
(84320, 'Jane', 15000),
(84560, 'Oscar', 175000);
Also have started a SQL Fiddle with the above: http://sqlfiddle.com/#!6/4ee0c/1