带有子查询的 Oracle PIVOT 子句示例

发布于 2024-12-25 10:18:04 字数 454 浏览 1 评论 0原文

Oracle 的PIVOT 子句的定义指定可以在 IN 子句中定义子查询。我想象的一个虚构的例子是这样的

... PIVOT (AVG(salary) FOR (company) IN (SELECT DISTINCT company FROM companies))

,但是,我收到一个ORA-00936:缺少表达式错误。不幸的是,这个新的 PIVOT 子句中的错误通常相当神秘。谁能给我一个很好的例子来说明如何在 PIVOT 子句的 IN 子句中使用子查询?

Oracle's definition of the PIVOT clause specifies that there is a possibility to define a subquery in the IN clause. A fictional example of what I would imagine this to be is this

... PIVOT (AVG(salary) FOR (company) IN (SELECT DISTINCT company FROM companies))

With that, however, I get an ORA-00936: Missing expression error. Unfortunately, errors from this new PIVOT clause are usually rather cryptic. Can anyone give me a good example of how a subquery can be used in the IN clause of the PIVOT clause?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

作死小能手 2025-01-01 10:18:04

显然,我懒得读到文档的结尾......更进一步,文档指出:

子查询 子查询仅与 XML 关键字结合使用。当您指定子查询时,子查询找到的所有值都将用于透视。 [...]

这将起作用

PIVOT XML (AVG(salary) FOR (company) IN (SELECT DISTINCT company FROM companies))

请参阅完整文档

http://docs.oracle.com/cd/B28359_01/server.111/b28286/ statements_10002.htm#CHDFAFIE

Apparently, I was too lazy to read to the end of the documentation... Further down, the documentation states:

subquery A subquery is used only in conjunction with the XML keyword. When you specify a subquery, all values found by the subquery are used for pivoting. [...]

This will work

PIVOT XML (AVG(salary) FOR (company) IN (SELECT DISTINCT company FROM companies))

See the full documentation

http://docs.oracle.com/cd/B28359_01/server.111/b28286/statements_10002.htm#CHDFAFIE

夕色琉璃 2025-01-01 10:18:04

我有类似的要求。我通过 pl sql 编写了一个动态 sql 并将其添加到数据透视 IN 子句中来实现这一点。当然pivot查询也是动态sql。但在普通的数据透视子句中,使用 sql 这是不可能的。

i had a similar requirement. I achieved this via pl sql wrote a dynamic sql and added it to the pivot IN clause. Ofcourse pivot query was also a dynamic sql. But in normal pivot clause this is not possible, using sql.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文