使用带有比较的子查询 - Oracle

问题描述 投票:1回答:1

我一直试图将结果操纵到这三个查询中,我不知道我在做什么是错的

列出参加不到三组的所有学术成员。

列出导致最大组数的学术ID

使用此查询(第一部分)

SELECT a.ID  , min(a.name) as Name  
FROM Academic a , researchGroup r 
WHERE count(r.managerID)>3
GROUP BY a.ID; 

但它似乎不起作用。

我有这个关系模式

researchGroup(name (P.R Key Composite) , codeD , mainResearchArea , managerID /* forgien key with AcademicStaff(ID) */ , labID (P.R Key Composite) )

AcademicStaff(ID {PR KEY} , name)

任何解决方案

sql subquery oracle10g
1个回答
1
投票

以下将为您提供学术列表和管理的研究组数量:

SELECT
    *

FROM
    (
        SELECT 
            ac.ID                   AS academic_id
            ,MAX(ac.name)           AS academic_name
            ,COUNT(rg.managerID)    AS num_groups_managed
            ,DENSE_RANK() OVER (ORDER BY COUNT(rg.managerID) DESC) AS academic_rank

        FROM 
            Academic AS ac

        INNER JOIN
            researchGroup AS rg
            ON (rg.managerID = ac.ID)

        GROUP BY 
            ac.ID

    ) AS subquery

WHERE
    --** uncomment the following line for the academics managing above 3 groups
    --num_groups_managed >= 3

    --** or uncomment the following line for the top-ranked academics (there could be more than 1)
    --academic_rank = 1

ORDER BY
    academic_rank ASC
    ,academic_name ASC

取消注释WHERE子句的相关部分将为您提供所需的结果。

顺便说一句,因为我使用过Oracle SQL已经有一段时间了,所以请原谅任何小的语法错误(特别是,我不记得Oracle是否在表名后接受了关键字AS)。

© www.soinside.com 2019 - 2024. All rights reserved.