oracle 标量子查询?
我有这样一个表:
name value1 value2 value3
---------------------------
name1 1 1 1
name2 1 1 2
name3 2 2 11
name4 2 12 2
name5 3 3 8
name6 3 3 2
我需要的是这样的结果:
name value1 value2 value3
---------------------------
name2 1 1 2
name4 2 12 2
name5 3 3 8
即:
- 每组
value1
有Exact 1 enty; - 该条目必须具有该组
value1
中value2
的最大值; - 该条目必须具有
value2
组中value3
的最大值。
在互联网上搜索后,我得到了一个解决方案,在 SELECT 列表中使用标量子查询作为单个列,但它非常难看且复杂,因为必须为每个列值1
、值2
、值3
。
SQL Cookbook 在配方 14.10 中提到了通过将类型定义为对象的解决方案,但是我更喜欢单个 SELECT 语句中的解决方案。
有什么简单的办法吗?
I have such a table:
name value1 value2 value3
---------------------------
name1 1 1 1
name2 1 1 2
name3 2 2 11
name4 2 12 2
name5 3 3 8
name6 3 3 2
what I need is such a result:
name value1 value2 value3
---------------------------
name2 1 1 2
name4 2 12 2
name5 3 3 8
I.e:
- Exact 1 enty for each group of
value1
; - This entry must have the max value of
value2
in this group ofvalue1
; - This entry must have the max value of
value3
in the group ofvalue2
.
After having searched in Internet, I have got a solution by using scalar subquery in SELECT
list as a single column, but it is very ugly and complex, since the same sub query must be run for each of the column value1
, value2
, value3
.
SQL Cookbook mentions a solution in recipe 14.10 via defining a type as object, but i prefer a solution in a single SELECT statement.
Any easy way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在这种情况下,分析是你的朋友:
Analytics are your friend in this case: