使用Oracle中的max函数查找记录

时间:2013-10-14 19:10:05

标签: oracle oracle10g subquery

好的,所以我在Oracle中有两个表:

CAR,具有属性所有者(引用EMPLOYEE(ssn))

EMPLOYEE,具有属性名称,ssn。

我需要获得具有最大数量汽车的员工的姓名和ssn,但我无法理解如何以这种方式使用聚合函数......

我尝试过以下内容:

    SELECT name, ssn
    FROM EMPLOYEE
    WHERE ssn IN (SELECT owner
                  FROM CAR
                  HAVING COUNT(owner) = MAX(COUNT(owner))
                  GROUP BY owner)

但它似乎没有返回任何东西......我是否在正确的轨道上?

2 个答案:

答案 0 :(得分:1)

select e.name,e.ssn,count(*)
from EMPLOYEE e 
inner join CAR c on e.ssn = c.owner
group by e.name,e.ssn

答案 1 :(得分:1)

select * from EMPLOYEE
where ssn = (
  select min(owner) keep (dense_rank first order by count(1) desc, owner) 
  from CAR 
  group by owner
)

fiddle