合并列
我正在尝试执行以下操作: 我有一张包含 ename、job、deptno 和 sal 的表。我正在尝试发起一个查询,返回每个部门的最高收入者。我已经通过分组和子查询完成了此操作。但是,我还想按部门显示平均萨尔。因此,结果如下:
"ename" "dept" "sal" "average of dept"
sal 20 1000 500
kelly 30 2000 800
mika 40 3000 400
这可能是不可能的,因为平均值不与其他行关联。
任何建议将不胜感激。谢谢。我正在使用 Oracle 10g 来运行我的查询。
I am trying to do the following:
I have a table with ename, job, deptno, and sal. I am trying to initiate a query that returns the top earners of each department. I have done this with grouping and a subquery. However, I also want to display the average sal by deptno. So the following would be the result:
"ename" "dept" "sal" "average of dept"
sal 20 1000 500
kelly 30 2000 800
mika 40 3000 400
this might be impossible since the average does not associate with the other rows.
any suggestion would be appreciated. Thanks. I am using Oracle 10g to run my queries.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用分析函数
:如果每个部门都具有相同的
sal
最大值,则每个部门可能会返回多名员工。如果您只想每个部门一个人,您可以将RANK()
替换为ROW_NUMBER()
(在这种情况下,您还可以进一步扩展ORDER BY
通过指定附加排序标准来选择顶部项目,否则将从最高工资中随机选择)。You could use analytic functions:
This may return more than one employee per department if all of them have the same maximum value of
sal
. You can replaceRANK()
withROW_NUMBER()
if you only want one person per department (in which case you could also further extendORDER BY
by specifying additional sorting criteria to pick the top item, otherwise it will be picked randomly from among those with the maximum salary).这应该有效。唯一的技巧是,如果一个部门中有几位最高薪的员工,它会显示所有员工。
This should work. The only trick is that if you have several employees with the maximum salary in a department, it will show all of them.
不确定 Oracle,大约 10 年没有使用过它,但类似这样的事情应该是可能的:
Not sure about Oracle, haven't used it in about 10 years, but something like this should be possible: