Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Recusive Total for Parent-Child table

Author  Topic 

kule
Starting Member

3 Posts

Posted - 2009-01-22 : 07:02:37
Hi There,

I'm getting really stuck on coming up with a solution for this problem. I've tried loops & CTEs however I'm getting into trouble because of the left join required to join company_groups & groups.

If anyone has a solution or can point me in the right direction I'd be grateful...

Many Thanks
Luke


CREATE TABLE #companies (
id int
, name varchar(50)
)

CREATE TABLE #company_groups (
company_id int
, group_id int
, ranking int
)

CREATE TABLE #groups (
id int
, parent_id int
, name varchar(50)
)


INSERT INTO #companies VALUES (1, 'Company X')
INSERT INTO #companies VALUES (2, 'Company Y')

INSERT INTO #groups VALUES (1, NULL, 'Group A')
INSERT INTO #groups VALUES (2, 1, 'Group A-1')
INSERT INTO #groups VALUES (3, 1, 'Group A-2')
INSERT INTO #groups VALUES (4, 2, 'Group A-1-1')
INSERT INTO #groups VALUES (5, NULL, 'Group B')
INSERT INTO #groups VALUES (6, NULL, 'Group C')
INSERT INTO #groups VALUES (7, 6, 'Group C-1')
INSERT INTO #groups VALUES (8, 7, 'Group C-1-1')

INSERT INTO #company_groups VALUES (1, 1, 60)
INSERT INTO #company_groups VALUES (1, 4, 40)
INSERT INTO #company_groups VALUES (2, 4, 10)
INSERT INTO #company_groups VALUES (2, 5, 80)
INSERT INTO #company_groups VALUES (2, 7, 90)
INSERT INTO #company_groups VALUES (2, 8, 30)

/*
EXAMPLE OF DESIRED OUTPUT

In this case we're selecting all the top level groups
(groups.parent_id IS NULL).

cumulative_ranking is the ranking for that company_group
and sum of all the rankings for the groups children
--------------------------------------------------
| company_name | group_name | cumulative_ranking |
--------------------------------------------------
| Company X | Group A | 100 |
| Company X | Group B | 0 |
| Company X | Group C | 0 |
| Company Y | Group A | 10 |
| Company Y | Group B | 80 |
| Company Y | Group C | 120 |
--------------------------------------------------

Notes:
* Children in the groups table can go to any depth
* The eventual solution will be queried using a
specific group_id and company_id.
*/

DROP TABLE #groups
DROP TABLE #company_groups
DROP TABLE #companies

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-01-22 : 09:26:23
make use of common table expression for this

http://msdn.microsoft.com/en-us/library/ms186243.aspx
Go to Top of Page

Padmaja
Starting Member

6 Posts

Posted - 2009-01-23 : 01:26:11
select distinct
t.name as company_name,
t.group_name ,
sum(isnull(t.cumulative_ranking,0)) as cumulative_ranking
from
(select distinct
c.name,
left(g.name,7) as group_name,
(select cg.ranking from #company_groups as cg where cg.id = c.id
and g.id = cg.group_id )as cumulative_ranking
from #companies as c
inner join #company_groups as cg on (cg.id = c.id)
inner join #groups as g on ( g.id = cg.group_id or g.parent_id is null)) as t
group by t.group_name ,t.name
Go to Top of Page

raky
Aged Yak Warrior

767 Posts

Posted - 2009-01-23 : 05:41:01
[code]
Hi,

declare @details table (gid int, gparent int, gname varchar(255),
gcompanyid int )
insert into @details
select distinct g.id,g.parent_id,left(g.name,7), cg.company_id from groups as g,
company_groups as cg
order by cg.company_id

select distinct c.name as company_name, d.gname as group_name, sum(isnull(cg.ranking,0))as cumulativeranking from @details as d
left join company_groups as cg on cg.group_id = d.gid
and d.gcompanyid = cg.company_id
inner join companies as c on c.id = d.gcompanyid
group by d.gname,c.name

Be cool..
[/code]
Go to Top of Page

kule
Starting Member

3 Posts

Posted - 2009-01-23 : 06:20:11
Thanks for all the replies guys.

Perhaps I should add that the name data is just *test data* the groups are actually various terms rather than the nice 'Group A', 'Group A-1' as I have below so filtering by name isn't a viable option.

Cheers
Luke
Go to Top of Page
   

- Advertisement -