gpt4 book ai didi

sql - 根据子查询显示列

转载 作者:行者123 更新时间:2023-12-04 05:14:14 25 4
gpt4 key购买 nike

我想显示工资最高的部门名称。
我使用的是oracle sql,表结构是Dept(Deptno,Dname,Loc)Emp(Empno,Ename,Job,Salary,Deptno) .

我使用的查询是

select Dname 
from Dept
where Deptno=
( select Deptno
from Emp
where rownum=1
group by Deptno
order by sum(Salary) Desc
);

这给出了一个错误:

Right parenthesis missing.



当我单独运行子查询时,它成功返回 Deptno .但是对于父查询,我收到了上述错误。

问题是什么,可能的解决方案是什么?

最佳答案

select dname
from (select dname, rank() over (order by sum(salary) desc) rnk
from dept d
inner join emp e
on e.deptno = d.deptno
group by dname, e.deptno
)
where rnk = 1;

请注意,在您的示例中输入 where rownum=1你所做的是一个巨大的错误。这意味着选择 1 个随机行并对其进行排序(不是真正的最高薪水行......只是任何旧行)

如果 2 个部门的最高工资相同,我的解决方案可能会超过 1 行。您可以使用 row_number()而不是 rank()如果你愿意,就选择一个。

关于sql - 根据子查询显示列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14496202/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com