EDIT: This answer gives incorrect results but I will leave it up for historical purposes.
I think this will help you out. I can't figure out how to get rid of the Distinct
though.
Select Distinct
Project.ProjectId,
Sum(Task.PlannedHours)
Over (Partition By Project.ProjectId) As PlannedHours,
Sum(Assignment.AssignedHours)
Over (Partition By Project.ProjectId) As AssignedHours
From Project
Inner Join Task On Project.ProjectId = Task.ProjectId
Inner Join Assignment On Task.TaskId= Assignment.AssignmentId
Here is my test script.
Declare @Project Table (ProjectId Int)
Declare @Task Table (TaskId Int, ProjectId Int, PlannedHours Int)
Declare @Assignment Table (AssignmentId Int, TaskId Int, AssignedHours Int)
Insert Into @Project (ProjectId) values (1)
Insert Into @Project (ProjectId) values (2)
Insert Into @Project (ProjectId) values (3)
Insert Into @Task (TaskId, ProjectId, PlannedHours) values (1, 1, 10)
Insert Into @Task (TaskId, ProjectId, PlannedHours) values (2, 1, 10)
Insert Into @Task (TaskId, ProjectId, PlannedHours) values (3, 2, 11)
Insert Into @Task (TaskId, ProjectId, PlannedHours) values (4, 3, 12)
Insert Into @Assignment (AssignmentId, TaskId, AssignedHours) values (1, 1, 2)
Insert Into @Assignment (AssignmentId, TaskId, AssignedHours) values (2, 1, 2)
Insert Into @Assignment (AssignmentId, TaskId, AssignedHours) values (3, 2, 2)
Insert Into @Assignment (AssignmentId, TaskId, AssignedHours) values (4, 3, 3)
Select Distinct
Project.ProjectId,
Sum(Task.PlannedHours)
Over (Partition By Project.ProjectId) As PlannedHours,
Sum(Assignment.AssignedHours)
Over (Partition By Project.ProjectId) As AssignedHours
From @Project Project
Inner Join @Task Task On Project.ProjectId = Task.ProjectId
Inner Join @Assignment Assignment On Task.TaskId= Assignment.AssignmentId