mirror of
				https://gitee.com/coder-xiaomo/leetcode-problemset
				synced 2025-11-04 11:43:12 +08:00 
			
		
		
		
	
		
			
				
	
	
		
			48 lines
		
	
	
		
			1.9 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
			
		
		
	
	
			48 lines
		
	
	
		
			1.9 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
<p>Table: <code>Employees</code></p>
 | 
						|
 | 
						|
<pre>
 | 
						|
+-------------+----------+
 | 
						|
| Column Name | Type     |
 | 
						|
+-------------+----------+
 | 
						|
| employee_id | int      |
 | 
						|
| name        | varchar  |
 | 
						|
| reports_to  | int      |
 | 
						|
| age         | int      |
 | 
						|
+-------------+----------+
 | 
						|
employee_id is the column with unique values for this table.
 | 
						|
This table contains information about the employees and the id of the manager they report to. Some employees do not report to anyone (reports_to is null). 
 | 
						|
</pre>
 | 
						|
 | 
						|
<p> </p>
 | 
						|
 | 
						|
<p>For this problem, we will consider a <strong>manager</strong> an employee who has at least 1 other employee reporting to them.</p>
 | 
						|
 | 
						|
<p>Write a solution to report the ids and the names of all <strong>managers</strong>, the number of employees who report <strong>directly</strong> to them, and the average age of the reports rounded to the nearest integer.</p>
 | 
						|
 | 
						|
<p>Return the result table ordered by <code>employee_id</code>.</p>
 | 
						|
 | 
						|
<p>The result format is in the following example.</p>
 | 
						|
 | 
						|
<p> </p>
 | 
						|
<p><strong class="example">Example 1:</strong></p>
 | 
						|
 | 
						|
<pre>
 | 
						|
<strong>Input:</strong> 
 | 
						|
Employees table:
 | 
						|
+-------------+---------+------------+-----+
 | 
						|
| employee_id | name    | reports_to | age |
 | 
						|
+-------------+---------+------------+-----+
 | 
						|
| 9           | Hercy   | null       | 43  |
 | 
						|
| 6           | Alice   | 9          | 41  |
 | 
						|
| 4           | Bob     | 9          | 36  |
 | 
						|
| 2           | Winston | null       | 37  |
 | 
						|
+-------------+---------+------------+-----+
 | 
						|
<strong>Output:</strong> 
 | 
						|
+-------------+-------+---------------+-------------+
 | 
						|
| employee_id | name  | reports_count | average_age |
 | 
						|
+-------------+-------+---------------+-------------+
 | 
						|
| 9           | Hercy | 2             | 39          |
 | 
						|
+-------------+-------+---------------+-------------+
 | 
						|
<strong>Explanation:</strong> Hercy has 2 people report directly to him, Alice and Bob. Their average age is (41+36)/2 = 38.5, which is 39 after rounding it to the nearest integer.
 | 
						|
</pre>
 |