try this:
DECLARE @Staff table (UserSrn char(4), UserName varchar(10), ManagerSrn char(4))
INSERT @Staff VALUES ('ABC1','Jerome', NULL )
INSERT @Staff VALUES ('ABC2','Joe' ,'ABC1')
INSERT @Staff VALUES ('ABC3','Paul' ,'ABC2')
INSERT @Staff VALUES ('ABC4','Jack' ,'ABC3')
INSERT @Staff VALUES ('ABC5','Daniel','ABC3')
INSERT @Staff VALUES ('ABC6','David' ,'ABC2')
INSERT @Staff VALUES ('ABC7','Ian' ,'ABC6')
INSERT @Staff VALUES ('ABC8','Helen' ,'ABC6')
DECLARE @SurveyResponses table (UserSrn char(4), QuestionId int, ResponseScore int)
INSERT @SurveyResponses VALUES ('ABC2',1 ,5)
INSERT @SurveyResponses VALUES ('ABC2',3 ,4)
INSERT @SurveyResponses VALUES ('ABC6',16,3)
DECLARE @RootUserSrn char(4)
SET @RootUserSrn='ABC2'
--get tree of given user
;WITH StaffTree AS
(
SELECT
UserSrn, UserName, ManagerSrn, UserSrn AS ManagerUserSrn, UserName AS ManagerUserName, 1 AS LevelOf
FROM @Staff
WHERE UserSrn=@RootUserSrn
UNION ALL
SELECT
s.UserSrn, s.UserName, s.ManagerSrn, t.UserSrn, t.UserName, t.LevelOf+1
FROM StaffTree t
INNER JOIN @Staff s ON t.UserSrn=s.ManagerSrn
WHERE s.ManagerSrn=@RootUserSrn
)
SELECT
s.UserName,COUNT(r.QuestionId) AS Completed,'???' as total
FROM StaffTree s
LEFT OUTER JOIN @SurveyResponses r ON s.UserSrn=r.UserSrn
GROUP BY s.UserName,s.LevelOf
ORDER BY s.LevelOf
OUTPUT:
UserName Completed total
---------- ----------- -----
Joe 2 ???
David 1 ???
Paul 0 ???
EDIT after OP's comments:
DECLARE @Staff table (UserSrn char(4), UserName varchar(10), ManagerSrn char(4))
INSERT @Staff VALUES ('ABC1','Jerome', NULL )
INSERT @Staff VALUES ('ABC2','Joe' ,'ABC1')
INSERT @Staff VALUES ('ABC3','Paul' ,'ABC2')
INSERT @Staff VALUES ('ABC4','Jack' ,'ABC3')
INSERT @Staff VALUES ('ABC5','Daniel','ABC3')
INSERT @Staff VALUES ('ABC6','David' ,'ABC2')
INSERT @Staff VALUES ('ABC7','Ian' ,'ABC6')
INSERT @Staff VALUES ('ABC8','Helen' ,'ABC6')
DECLARE @SurveyResponses table (UserSrn char(4), QuestionId int, ResponseScore int)
INSERT @SurveyResponses VALUES ('ABC2',1 ,5)
INSERT @SurveyResponses VALUES ('ABC2',3 ,4)
INSERT @SurveyResponses VALUES ('ABC6',16,3)
DECLARE @RootUserSrn char(4)
SET @RootUserSrn='ABC2'
--get tree of given user
;WITH StaffTree AS
(
SELECT
UserSrn, UserName, ManagerSrn, UserSrn AS ManagerUserSrn, UserName AS ManagerUserName, 1 AS LevelOf
FROM @Staff
WHERE UserSrn=@RootUserSrn
UNION ALL
SELECT
s.UserSrn, s.UserName, s.ManagerSrn, t.UserSrn, t.UserName, t.LevelOf+1
FROM StaffTree t
INNER JOIN @Staff s ON t.UserSrn=s.ManagerSrn
WHERE s.ManagerSrn=@RootUserSrn
)
, MINLevel AS (
SELECT MIN(LevelOf) AS MinLevelOf FROM StaffTree
)
, TotalLevel AS (
SELECT
SUM(CASE WHEN s.LevelOf !=m.MinLevelOf THEN 1 ELSE 0 END) AS TotalOf
FROM StaffTree s
CROSS JOIN MINLevel m
)
,Results AS (
SELECT
s.UserName,SUM(CASE WHEN r.QuestionId=16 THEN 1 ELSE 0 END) AS Completed,t.TotalOf as total,s.LevelOf
FROM StaffTree s
LEFT OUTER JOIN @SurveyResponses r ON s.UserSrn=r.UserSrn
CROSS JOIN TotalLevel t
GROUP BY s.UserName,s.LevelOf,t.TotalOf
)
SELECT
UserName,Completed,total, 1,LevelOf
FROM Results
UNION ALL
SELECT
'TOTAL',SUM(Completed),SUM(total),2,0
FROM Results
ORDER BY 4,5
OUTPUT:
UserName Completed total LevelOf
---------- ----------- ----------- ----------- -----------
Joe 0 2 1 1
David 1 2 1 2
Paul 0 2 1 2
TOTAL 1 6 2 0
(4 row(s) affected)
I still can not see how the given data, results in Joe having completed=2 and Paul having completed 1. I changed the given data from ('ABC4',16,3)
to ('ABC6',16,3)
so someone in the result set would have one completed.