oracle SQL - unpivot



你能告诉我为什么它不起作用吗?:(

select *
from( select r.region_id, c.country_id
from countries c join regions r on r.region_id = c.region_id)
unpivot(
valuee for columnValue in (r.region_id))

ORA-01748:此处只允许使用简单列名01748.00000-"此处只允许使用简单列名">

使用此部分:

select *

您正在从内部选择中选择列:region_idcountry_id。因此,您的UNPIVOT部分不需要r.region_id,只需要region_id。此代码正确(无错误(:

select *
from(select r.region_id
, c.country_id
from countries c 
join regions r on r.region_id = c.region_id)
unpivot(valuee for columnValue in (region_id));

您的查询很奇怪,而且并不清楚您想要实现什么;但在@VBoka对你的代码进行更正后,你实际上根本不需要加入(除非你有不存在地区的国家(-你可以:

select *
from (
select region_id, country_id
from countries
)
unpivot(valuee for columnValue in (region_id));

但您可以在不取消平移的情况下获得相同的结果;加入,如果你有真正的理由包括它:

select c.country_id, 'REGION_ID' as columnvalue, r.region_id as valuee
from countries c
join regions r on r.region_id = c.region_id;

或不加入:

select country_id, 'REGION_ID' as columnvalue, region_id as valuee
from countries;

无论哪种方式,你都会得到一个结果集,每个国家都有一行。

最新更新