美文网首页
找到有最多员工的项目(多个最大值)

找到有最多员工的项目(多个最大值)

作者: 笃见弥月 | 来源:发表于2021-12-09 18:05 被阅读0次

    LeetCode 1076题


    project
    image.png

    1、先找到最大的数是多少,再分组筛选数量=最大的项目id

    SELECT project_id
    FROM Project
    GROUP BY project_id
    HAVING COUNT(*) = (
               SELECT COUNT(*)     #找到最大的数字
               FROM Project
               GROUP BY project_id
               ORDER BY COUNT(*) DESC
               LIMIT 1
                )
    

    2、筛选条件≥ALL(子查询)

    SELECT project_id
    FROM Project
    GROUP BY project_id
    HAVING COUNT(*) >= ALL (
            SELECT COUNT(*)    
            FROM Project
            GROUP BY project_id
            )
    

    3、窗口函数

    (注意group by和partition by用法)

    SELECT project_id
    FROM (
        SELECT project_id, rank() OVER(ORDER BY COUNT(employee_id) DESC) rk
        FROM Project
        GROUP BY project_id
    ) t
    WHERE t.rk=1
    

    相关文章

      网友评论

          本文标题:找到有最多员工的项目(多个最大值)

          本文链接:https://www.haomeiwen.com/subject/uoiffrtx.html