 #total level=8
 #current level=3
 
-(SELECT T.directReportees AS directReportees, sum(T.count) AS count
+(
+ #For level 3; inner unions = total levels - this level; max no. of joins = total level - current level - 1; 
+ SELECT T.directReportees AS directReportees, sum(T.count) AS count
+ FROM (    
+	SELECT manager.person_id AS directReportees, 0 AS count
+    FROM person_direct_reportee manager
+    WHERE manager.person_id = (SELECT id
+                               FROM person
+                               WHERE name = "first1 last7")
+	
+	UNION
+  
+	SELECT manager.person_id AS directReportees, count(manager.directly_manages) AS count
+    FROM person_direct_reportee manager
+    WHERE manager.person_id = (SELECT id
+                               FROM person
+                               WHERE name = "first1 last7")
+	
+	UNION
+	
+	SELECT manager.person_id AS directReportees, count(reportee.directly_manages) AS count
+    FROM person_direct_reportee manager
+    JOIN person_direct_reportee reportee
+    ON manager.directly_manages = reportee.person_id
+    WHERE manager.person_id = (SELECT id
+                               FROM person
+                               WHERE name = "first1 last7")
+    
+	
+    UNION                           
+    
+    SELECT manager.person_id AS directReportees, count(depth2Reportees.directly_manages) AS count
+    FROM person_direct_reportee manager 
+    JOIN person_direct_reportee depth1Reportees
+    ON manager.directly_manages = depth1Reportees.person_id
+        JOIN person_direct_reportee depth2Reportees
+        ON depth1Reportees.directly_manages = depth2Reportees.person_id
+    WHERE manager.person_id = (SELECT id
+                               FROM person
+                               WHERE name = "first1 last7")
+    
+    
+	UNION
+    
+	SELECT manager.person_id AS directReportees, count(depth3Reportees.directly_manages) AS count
+    FROM person_direct_reportee manager 
+    JOIN person_direct_reportee depth1Reportees
+    ON manager.directly_manages = depth1Reportees.person_id
+        JOIN person_direct_reportee depth2Reportees
+        ON depth1Reportees.directly_manages = depth2Reportees.person_id
+            JOIN person_direct_reportee depth3Reportees
+            ON depth2Reportees.directly_manages = depth3Reportees.person_id
+    WHERE manager.person_id = (SELECT id
+                               FROM person
+                               WHERE name = "first1 last7")
+    
+	
+	UNION
+    
+    SELECT manager.person_id AS directReportees, count(depth4Reportees.directly_manages) AS count
+    FROM person_direct_reportee manager 
+    JOIN person_direct_reportee depth1Reportees
+    ON manager.directly_manages = depth1Reportees.person_id
+        JOIN person_direct_reportee depth2Reportees
+        ON depth1Reportees.directly_manages = depth2Reportees.person_id
+            JOIN person_direct_reportee depth3Reportees
+            ON depth2Reportees.directly_manages = depth3Reportees.person_id
+                JOIN person_direct_reportee depth4Reportees
+                ON depth3Reportees.directly_manages = depth4Reportees.person_id
+    WHERE manager.person_id = (SELECT id
+                               FROM person
+                               WHERE name = "first1 last7")
+  ) AS T
+)
+
+UNION
+
+(
+ #For level 4; inner unions = total levels - this level; max no. of joins = total level - current level - 1; 
+ SELECT T.directReportees AS directReportees, sum(T.count) AS count
  FROM (    
   
-	SELECT manager.person_id AS directReportees, 0 AS count
-    FROM person_direct_reportee manager
-    WHERE manager.person_id = (SELECT id
-                               FROM person
-                               WHERE name = "first1 last17")
-	
-	UNION
-	
-	SELECT manager.person_id AS directReportees, count(manager.directly_manages) AS count
-    FROM person_direct_reportee manager
-    WHERE manager.person_id = (SELECT id
-                               FROM person
-                               WHERE name = "first1 last17")   
-    UNION                           
-    
-    SELECT manager.person_id AS directReportees, count(reportee.directly_manages) AS count
+	SELECT manager.directly_manages AS directReportees, 0 AS count
+    FROM person_direct_reportee manager
+    WHERE manager.person_id = (SELECT id
+                               FROM person
+                               WHERE name = "first1 last7")
+	
+	UNION
+	
+	SELECT reportee.person_id AS directReportees, count(reportee.directly_manages) AS count
     FROM person_direct_reportee manager
     JOIN person_direct_reportee reportee
     ON manager.directly_manages = reportee.person_id
     WHERE manager.person_id = (SELECT id
                                FROM person
-                               WHERE name = "first1 last17")
-    GROUP BY directReportees
-    
-	UNION
-    
-	SELECT manager.person_id AS directReportees, count(depth2Reportees.directly_manages) AS count
-    FROM person_direct_reportee manager 
-    JOIN person_direct_reportee depth1Reportees
-    ON manager.directly_manages = depth1Reportees.person_id
-        JOIN person_direct_reportee depth2Reportees
-        ON depth1Reportees.directly_manages = depth2Reportees.person_id
-    WHERE manager.person_id = (SELECT id
-                               FROM person
-                               WHERE name = "first1 last17")
-    GROUP BY directReportees
-	
-	UNION
-    
-    SELECT manager.person_id AS directReportees, count(depth3Reportees.directly_manages) AS count
-    FROM person_direct_reportee manager 
-    JOIN person_direct_reportee depth1Reportees
-    ON manager.directly_manages = depth1Reportees.person_id
-        JOIN person_direct_reportee depth2Reportees
-        ON depth1Reportees.directly_manages = depth2Reportees.person_id
-            JOIN person_direct_reportee depth3Reportees
-            ON depth2Reportees.directly_manages = depth3Reportees.person_id
-    WHERE manager.person_id = (SELECT id
-                               FROM person
-                               WHERE name = "first1 last17")
+                               WHERE name = "first1 last7")
+    GROUP BY directReportees
+	
+    UNION                           
+    
+    SELECT depth1Reportees.person_id AS directReportees, count(depth2Reportees.directly_manages) AS count
+    FROM person_direct_reportee manager 
+    JOIN person_direct_reportee depth1Reportees
+    ON manager.directly_manages = depth1Reportees.person_id
+        JOIN person_direct_reportee depth2Reportees
+        ON depth1Reportees.directly_manages = depth2Reportees.person_id
+    WHERE manager.person_id = (SELECT id
+                               FROM person
+                               WHERE name = "first1 last7")
+    GROUP BY directReportees
+    
+	UNION
+    
+	SELECT depth1Reportees.person_id AS directReportees, count(depth3Reportees.directly_manages) AS count
+    FROM person_direct_reportee manager 
+    JOIN person_direct_reportee depth1Reportees
+    ON manager.directly_manages = depth1Reportees.person_id
+        JOIN person_direct_reportee depth2Reportees
+        ON depth1Reportees.directly_manages = depth2Reportees.person_id
+            JOIN person_direct_reportee depth3Reportees
+            ON depth2Reportees.directly_manages = depth3Reportees.person_id
+    WHERE manager.person_id = (SELECT id
+                               FROM person
+                               WHERE name = "first1 last7")
+    GROUP BY directReportees
+	
+	UNION
+    
+    SELECT depth1Reportees.person_id AS directReportees, count(depth4Reportees.directly_manages) AS count
+    FROM person_direct_reportee manager 
+    JOIN person_direct_reportee depth1Reportees
+    ON manager.directly_manages = depth1Reportees.person_id
+        JOIN person_direct_reportee depth2Reportees
+        ON depth1Reportees.directly_manages = depth2Reportees.person_id
+            JOIN person_direct_reportee depth3Reportees
+            ON depth2Reportees.directly_manages = depth3Reportees.person_id
+                JOIN person_direct_reportee depth4Reportees
+                ON depth3Reportees.directly_manages = depth4Reportees.person_id
+    WHERE manager.person_id = (SELECT id
+                               FROM person
+                               WHERE name = "first1 last7")
     GROUP BY directReportees
   ) AS T
  GROUP BY directReportees

#For level 5; inner unions = total levels - this level; max no. of joins = total level - current level - 1; 
(SELECT T.directReportees AS directReportees, sum(T.count) AS count
 FROM (    
	>>>> TODO : continue from here
	SELECT manager.directly_manages AS directReportees, 0 AS count
    FROM person_direct_reportee manager
    WHERE manager.person_id = (SELECT id
                               FROM person
                               WHERE name = "first1 last17")   
    UNION                           
    
    SELECT depth2Reportees.person_id AS directReportees, count(depth2Reportees.directly_manages) AS count
    FROM person_direct_reportee manager 

GROUP BY directReportees 
)                          
UNION
(   #For level 8; inner unions = total levels - this level; max no. of joins = total level - current level - 1;
 
    SELECT depth2Reportees.directly_manages AS directReportees, 0 AS count
    FROM person_direct_reportee manager 
