Oracle 分析函数 - 如何重用 PARTITION BY 子句?
我用 Oracle 编写了一个复杂的 SQL 查询,其中我想在同一分区上使用两个分析函数。
让我们变得非常简单,但不要太过分:
SELECT col1,
MAX(col2) OVER(PARTITION BY col3, col4, col5, col6,
CASE WHEN col7 LIKE 'foo'
THEN SUBSTR(col7,1,5)
ELSE col7
END
ORDER BY col5 ASC, col6 DESC),
MIN(col2) OVER(PARTITION BY col3, col4, col5, col6,
CASE WHEN col7 LIKE 'foo'
THEN SUBSTR(col7,1,5)
ELSE col7
END
ORDER BY col5 ASC, col6 DESC)
FROM my_table;
是否有更优雅的语法来分解 PARTITION BY 子句?
谢谢。
I have written a complex SQL query with Oracle in which I want to use two analytic function over the same partition.
Let's be quite simple but not too much:
SELECT col1,
MAX(col2) OVER(PARTITION BY col3, col4, col5, col6,
CASE WHEN col7 LIKE 'foo'
THEN SUBSTR(col7,1,5)
ELSE col7
END
ORDER BY col5 ASC, col6 DESC),
MIN(col2) OVER(PARTITION BY col3, col4, col5, col6,
CASE WHEN col7 LIKE 'foo'
THEN SUBSTR(col7,1,5)
ELSE col7
END
ORDER BY col5 ASC, col6 DESC)
FROM my_table;
Is there a more elegant syntax for factoring the PARTITION BY
clause?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您指的是这样的标准 WINDOW 子句:
那么我相信答案是否,Oracle 不支持此(使用 11gR2 检查)。
If you are referring to the standard WINDOW clause like this:
then I believe the answer is no, Oracle does not support this (checked with 11gR2).
您可以使用子查询分解,也称为 with 子句:(
未经测试)
问候,
抢。
You can use subquery factoring, also known as the with-clause:
(untested)
Regards,
Rob.
分区定义可以与 WINDOW 子句一起重用。从 Oracle 20c 版本开始支持它:
查询可以重写为:
请注意,窗口定义的一部分可以扩展,例如查询可以共享
PARTITION BY
,但是有不同的排序:我们不能执行共享相同的 PARTITION BY 和 ORDER BY 但窗口大小不同的操作:
Partition definition could be reused with
WINDOW
clause. Starting from version 20c Oracle supports it:Query could be rewritten as:
Note that part of window definition could be extended, for instance queries could share
PARTITION BY
but have different sorting:We cannot perform sth like sharing the same
PARTITION BY
andORDER BY
but with different window size: