{ "data": { "question": { "questionId": "1882", "questionFrontendId": "1731", "categoryTitle": "Database", "boundTopicId": 572194, "title": "The Number of Employees Which Report to Each Employee", "titleSlug": "the-number-of-employees-which-report-to-each-employee", "content": "

Table: Employees

\n\n
\n+-------------+----------+\n| Column Name | Type     |\n+-------------+----------+\n| employee_id | int      |\n| name        | varchar  |\n| reports_to  | int      |\n| age         | int      |\n+-------------+----------+\nemployee_id is the column with unique values for this table.\nThis 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). \n
\n\n

 

\n\n

For this problem, we will consider a manager an employee who has at least 1 other employee reporting to them.

\n\n

Write a solution to report the ids and the names of all managers, the number of employees who report directly to them, and the average age of the reports rounded to the nearest integer.

\n\n

Return the result table ordered by employee_id.

\n\n

The result format is in the following example.

\n\n

 

\n

Example 1:

\n\n
\nInput: \nEmployees table:\n+-------------+---------+------------+-----+\n| employee_id | name    | reports_to | age |\n+-------------+---------+------------+-----+\n| 9           | Hercy   | null       | 43  |\n| 6           | Alice   | 9          | 41  |\n| 4           | Bob     | 9          | 36  |\n| 2           | Winston | null       | 37  |\n+-------------+---------+------------+-----+\nOutput: \n+-------------+-------+---------------+-------------+\n| employee_id | name  | reports_count | average_age |\n+-------------+-------+---------------+-------------+\n| 9           | Hercy | 2             | 39          |\n+-------------+-------+---------------+-------------+\nExplanation: 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.\n
\n", "translatedTitle": "每位经理的下属员工数量", "translatedContent": "

Table: Employees

\n\n
+-------------+----------+\n| Column Name | Type     |\n+-------------+----------+\n| employee_id | int      |\n| name        | varchar  |\n| reports_to  | int      |\n| age         | int      |\n+-------------+----------+\nemployee_id 是这个表的主键.\n该表包含员工以及需要听取他们汇报的上级经理的ID的信息。 有些员工不需要向任何人汇报(reports_to 为空)。\n
\n\n

 

\n\n

对于此问题,我们将至少有一个其他员工需要向他汇报的员工,视为一个经理。

\n\n

编写SQL查询需要听取汇报的所有经理的ID、名称、直接向该经理汇报的员工人数,以及这些员工的平均年龄,其中该平均年龄需要四舍五入到最接近的整数。

\n\n

返回的结果集需要按照 employee_id 进行排序。

\n\n

查询结果的格式如下:

\n\n

 

\n\n
Employees table:\n+-------------+---------+------------+-----+\n| employee_id | name    | reports_to | age |\n+-------------+---------+------------+-----+\n| 9           | Hercy   | null       | 43  |\n| 6           | Alice   | 9          | 41  |\n| 4           | Bob     | 9          | 36  |\n| 2           | Winston | null       | 37  |\n+-------------+---------+------------+-----+\n\nResult table:\n+-------------+-------+---------------+-------------+\n| employee_id | name  | reports_count | average_age |\n+-------------+-------+---------------+-------------+\n| 9           | Hercy | 2             | 39          |\n+-------------+-------+---------------+-------------+\nHercy 有两个需要向他汇报的员工, 他们是 Alice and Bob. 他们的平均年龄是 (41+36)/2 = 38.5, 四舍五入的结果是 39.\n
\n", "isPaidOnly": false, "difficulty": "Easy", "likes": 46, "dislikes": 0, "isLiked": null, "similarQuestions": "[]", "contributors": [], "langToValidPlayground": "{\"cpp\": false, \"java\": false, \"python\": false, \"python3\": false, \"mysql\": false, \"mssql\": false, \"oraclesql\": false, \"c\": false, \"csharp\": false, \"javascript\": false, \"typescript\": false, \"bash\": false, \"php\": false, \"swift\": false, \"kotlin\": false, \"dart\": false, \"golang\": false, \"ruby\": false, \"scala\": false, \"html\": false, \"pythonml\": false, \"rust\": false, \"racket\": false, \"erlang\": false, \"elixir\": false, \"pythondata\": false, \"react\": false, \"vanillajs\": false, \"postgresql\": false}", "topicTags": [ { "name": "Database", "slug": "database", "translatedName": "数据库", "__typename": "TopicTagNode" } ], "companyTagStats": null, "codeSnippets": [ { "lang": "MySQL", "langSlug": "mysql", "code": "# Write your MySQL query statement below", "__typename": "CodeSnippetNode" }, { "lang": "MS SQL Server", "langSlug": "mssql", "code": "/* Write your T-SQL query statement below */", "__typename": "CodeSnippetNode" }, { "lang": "Oracle", "langSlug": "oraclesql", "code": "/* Write your PL/SQL query statement below */", "__typename": "CodeSnippetNode" }, { "lang": "Pandas", "langSlug": "pythondata", "code": "import pandas as pd\n\ndef count_employees(employees: pd.DataFrame) -> pd.DataFrame:\n ", "__typename": "CodeSnippetNode" }, { "lang": "PostgreSQL", "langSlug": "postgresql", "code": "-- Write your PostgreSQL query statement below", "__typename": "CodeSnippetNode" } ], "stats": "{\"totalAccepted\": \"19.1K\", \"totalSubmission\": \"41.4K\", \"totalAcceptedRaw\": 19145, \"totalSubmissionRaw\": 41425, \"acRate\": \"46.2%\"}", "hints": [], "solution": null, "status": null, "sampleTestCase": "{\"headers\":{\"Employees\":[\"employee_id\",\"name\",\"reports_to\",\"age\"]},\"rows\":{\"Employees\":[[9,\"Hercy\",null,43],[6,\"Alice\",9,41],[4,\"Bob\",9,36],[2,\"Winston\",null,37]]}}", "metaData": "{\"mysql\":[\"Create table If Not Exists Employees(employee_id int, name varchar(20), reports_to int, age int)\"],\"mssql\":[\"Create table Employees(employee_id int, name varchar(20), reports_to int, age int)\"],\"oraclesql\":[\"Create table Employees(employee_id int, name varchar(20), reports_to int, age int)\"],\"database\":true,\"name\":\"count_employees\",\"pythondata\":[\"Employees = pd.DataFrame([], columns=['employee_id', 'name', 'reports_to', 'age']).astype({'employee_id':'Int64', 'name':'object', 'reports_to':'Int64', 'age':'Int64'})\"],\"postgresql\":[\"Create table If Not Exists Employees(employee_id int, name varchar(20), reports_to int, age int)\"],\"database_schema\":{\"Employees\":{\"employee_id\":\"INT\",\"name\":\"VARCHAR(20)\",\"reports_to\":\"INT\",\"age\":\"INT\"}}}", "judgerAvailable": true, "judgeType": "large", "mysqlSchemas": [ "Create table If Not Exists Employees(employee_id int, name varchar(20), reports_to int, age int)", "Truncate table Employees", "insert into Employees (employee_id, name, reports_to, age) values ('9', 'Hercy', 'None', '43')", "insert into Employees (employee_id, name, reports_to, age) values ('6', 'Alice', '9', '41')", "insert into Employees (employee_id, name, reports_to, age) values ('4', 'Bob', '9', '36')", "insert into Employees (employee_id, name, reports_to, age) values ('2', 'Winston', 'None', '37')" ], "enableRunCode": true, "envInfo": "{\"mysql\":[\"MySQL\",\"

\\u7248\\u672c\\uff1aMySQL 8.0<\\/code><\\/p>\"],\"mssql\":[\"MS SQL Server\",\"

mssql server 2019.<\\/p>\"],\"oraclesql\":[\"Oracle\",\"

Oracle Sql 11.2.<\\/p>\"],\"pythondata\":[\"Pandas\",\"

Python 3.10 with Pandas 2.0.2 and NumPy 1.25.0<\\/p>\"],\"postgresql\":[\"PostgreSQL\",\"

PostgreSQL 16<\\/p>\"]}", "book": null, "isSubscribed": false, "isDailyQuestion": false, "dailyRecordStatus": null, "editorType": "CKEDITOR", "ugcQuestionId": null, "style": "LEETCODE", "exampleTestcases": "{\"headers\":{\"Employees\":[\"employee_id\",\"name\",\"reports_to\",\"age\"]},\"rows\":{\"Employees\":[[9,\"Hercy\",null,43],[6,\"Alice\",9,41],[4,\"Bob\",9,36],[2,\"Winston\",null,37]]}}", "__typename": "QuestionNode" } } }