zoukankan      html  css  js  c++  java
  • 570. Managers with at Least 5 Direct Reports 至少有5个直接汇报员工的经理

    The Employee table holds all employees including their managers. Every employee has an Id, and there is also a column for the manager Id.

    +------+----------+-----------+----------+
    |Id    |Name 	  |Department |ManagerId |
    +------+----------+-----------+----------+
    |101   |John 	  |A 	      |null      |
    |102   |Dan 	  |A 	      |101       |
    |103   |James 	  |A 	      |101       |
    |104   |Amy 	  |A 	      |101       |
    |105   |Anne 	  |A 	      |101       |
    |106   |Ron 	  |B 	      |101       |
    +------+----------+-----------+----------+
    

    Given the Employee table, write a SQL query that finds out managers with at least 5 direct report. For the above table, your SQL query should return:

    +-------+
    | Name  |
    +-------+
    | John  |
    +-------+
    

    Note:
    No one would report to himself.

    给一个员工表和一个经理表,员工表里有经理id列,查询出所有至少有5个直接汇报员工的经理。

    解法1:

    SELECT
        Name
    FROM
        Employee AS t1 JOIN
        (SELECT
            ManagerId
        FROM
            Employee
        GROUP BY ManagerId
        HAVING COUNT(ManagerId) >= 5) AS t2
        ON t1.Id = t2.ManagerId
    ;  

    解法2:

    with cte as(
    select ManagerId from employee
    group by ManagerId
    having count(Id)>=5
    ) select Name from employee e join cte c on e.Id=c.ManagerId  

    All LeetCode Questions List 题目汇总

  • 相关阅读:
    SpringBoot笔记
    SpringBoot面试篇
    多线程篇
    Tomcat篇
    Redis篇
    Nginx篇
    JVM篇
    MySQL篇
    python ETL工具 pyetl
    python通用数据库操作工具 pydbclib
  • 原文地址:https://www.cnblogs.com/lightwindy/p/9698913.html
Copyright © 2011-2022 走看看