简体   繁体   中英

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. I want to see the actual numbers!

You can't use result of subquery in PIVOT clause as columns need to be defined statically. But you can generate dynamic SQL using results of your subquery and then execute it via execute immediate.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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