获取与 Oracle 中的最大和最小行相关的值
在 Oracle 11g 中,我们需要能够查询表以从特定组中具有最高值和最低值的行中提取信息。例如,使用 EMP 表,我们希望找到每个部门中工资最高的人员的姓名和工资最低的人员的姓名
DEPTNO MAX_SAL MAX_EARNER MIN_SAL MIN_EARNER
-------------------------------------------------------
10 5000 KING 1300 MILLER
20 3000 FORD 2975 JONES
etc
(如果有两名或两名以上工资最高或最低的员工,我们希望始终按字母顺序返回第一个)。
上一篇文章讨论了如何仅获取最大值的值,但不能同时获取最大值和最小值。
目前我们有一个基于上面链接的不整洁的解决方案,然后应用后续查询,但性能对我们来说很重要。我预测一个好的解决方案还需要分析函数,并且可能需要一个将多行合并为单行的枢轴。
非常感谢任何帮助! 理查德
In Oracle 11g we need to be able to query a table to pull out information from rows with the highest and lowest values in a certain group. For example using the EMP table we'd like to find the name of person with the highest salary and the name of the person with the lowest salary in each department
DEPTNO MAX_SAL MAX_EARNER MIN_SAL MIN_EARNER
-------------------------------------------------------
10 5000 KING 1300 MILLER
20 3000 FORD 2975 JONES
etc
(if there are two or more staff with the highest or lowest salaray we want to always return the first in alphabetical order).
A Previous Post discussed how to get a value for just the maximum but not both max and min.
We have an untidy solution at the moment based on the link above then applying subsiquent queries but performance is important to us. I predict a good solution will also require analytic functions and possibly a pivot to coalesce the multiple rows into single rows.
Any help greatly appreciated!
Richard
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这可以通过分析函数轻松解决。正如您所看到的,DEPT 20 有两名员工的工资最高;这是一个重要的细节,因为此类问题的一些常见解决方案会忽略该信息。
糟糕,我错过了有关结果格式的重要细节。我的数据不符合要求的输出,因为有两名员工赚取最高工资。所以这个查询(我承认有点尴尬)为我们提供了所需的布局。员工姓名上的 MIN() 返回字母顺序:
我不喜欢这个解决方案。大多数数据集都会包含此类冲突,我们需要承认它们。根据一些不相关的标准过滤结果以适应 Procrustean 报告布局是具有误导性的。我更喜欢反映整个数据集的报告布局。最终,这取决于查询所服务的业务目的。当然,客户永远是对的 8-)
This is easily solvable with analytic functions. As you can see, there are two employees earning the maximum salary in DEPT 20; this is an important detail, as some common solutions to this kind of problem miss that information.
Oops, I missed an important detail about the result format. My data won't fit the requested output, because there are two employees earning the maximum salary. So this query, which I admit is a bit awkward, gives us the required layout. The MIN() on the employee names returns the alphabetical order :
I don't like this solution. Most datasets will contain such clashes, and we need to acknowledge them. Filtering the result on the basis of some unrelated criteria to fit a Procrustean report layout is misleading. I would prefer a report layout which reflected the whole dataset. Ultimately it depends on the business purpose which the query serves. And, of course, the customer is always right 8-)
您可以使用以下查询,
假设您的表格如下所示:
更新
为了满足您的其他要求“如果有两名或更多员工的工资最高或最低,我们希望始终返回第一个按字母顺序排列”,您可以按如下方式稍微调整查询(我确信这里还有改进的余地):
You can use the below query
assuming your table looks like this:
Update
In order to acheive your other requirement "if there are two or more staff with the highest or lowest salaray we want to always return the first in alphabetical order", you may tweak the query a bit as below (I'm sure there's scope for improvement here):