When dealing with hierarchical data like this, it can be useful to create a function which returns the 'path' of ancestors. You can then use that path in various ways...-- Structure and datacreate table MyProject (ID int, Name varchar(30), parentID int)insert MyProject select 1, 'Program14', Nullunion all select 2, 'Project1', 1union all select 3, 'Taskorder1', 2union all select 4, 'Taskorder2', 2create table MySurvey (projectID int, monthID int, questionID int, scoreID int)insert MySurvey select 3, 1, 1, 1union all select 3, 1, 2, 1union all select 3, 1, 3, 2union all select 3, 1, 4, 3union all select 3, 1, 5, 2union all select 4, 1, 1, 1union all select 4, 1, 2, 1union all select 4, 1, 3, 2union all select 4, 1, 4, 3union all select 4, 1, 5, 2go-- Functioncreate function dbo.AncestorPath(@ID int) returns varchar(100) asbegin declare @Path varchar(100) while 0 = 0 begin select @Path = cast(ID as varchar(5)) + isnull('/' + @Path, ''), @ID = parentID from dbo.MyProject where ID = @ID if @@rowcount = 0 break end return @Pathendgo-- Calculationselect ID, scoreID, count(*) as cnt, dbo.AncestorPath(ID) as Pathfrom MyProject a left outer join MySurvey b on '/' + dbo.AncestorPath(b.projectID) + '/' like '%/' + cast(a.ID as varchar(10)) + '/%'group by ID, scoreID order by ID, scoreID/* ResultsID scoreID cnt Path----------- ----------- ----------- ----------1 1 4 11 2 4 11 3 2 12 1 4 1/22 2 4 1/22 3 2 1/23 1 2 1/2/33 2 2 1/2/33 3 1 1/2/34 1 2 1/2/44 2 2 1/2/44 3 1 1/2/4*/
Ryan Randall Solutions are easy. Understanding the problem, now, that's the hard part.