简体   繁体   English

使用子查询Oracle SQL进行数据透视

[英]Pivot with subquery oracle sql

I am trying to use pivot to display my information. 我正在尝试使用数据透视显示我的信息。 I want to use a subquery to select the columns in the report: 我想使用子查询来选择报告中的列:

select * from 
(select continent, country, population
 from continents, countries
 where continents.id=countries.id
 )
 pivot (sum(population) for country in (select distinct country from countries)

I keep getting an error when I try to use a subquery to get the list of countries. 尝试使用子查询获取国家/地区列表时,我总是收到错误消息。

I tried using pivot XML but that didn't work either since it returned meaningless xml code. 我尝试使用数据透视XML,但由于它返回了无意义的xml代码,因此也不起作用。 I want to see the actual numbers! 我想看看实际数字!

You can't use result of subquery in PIVOT clause as columns need to be defined statically. 您不能在PIVOT子句中使用子查询的结果,因为需要静态定义列。 But you can generate dynamic SQL using results of your subquery and then execute it via execute immediate. 但是,您可以使用子查询的结果生成动态SQL,然后通过execute立即执行它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM