【问题标题】:oracle SQL - unpivotoracle SQL - 取消透视
【发布时间】:2020-04-02 15:40:39
【问题描述】:

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

    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 - “这里只允许简单的列名”

【问题讨论】:

标签: sql oracle oracle11g unpivot


【解决方案1】:

这部分:

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));

【讨论】:

  • 嗨@MateuszOsiński,不客气。乐意效劳。如果您不介意,请接受我的正确回答。您可以这样做:stackoverflow.com/help/someone-answers 干杯!
  • 嗨@MateuszOsiński,您还没有将此答案标记为正确吗?
【解决方案2】:

您的查询很奇怪,不清楚您要达到什么目的;但是跟进@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;

无论哪种方式,您都会得到一个每个国家/地区都有一行的结果集。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-19
    • 1970-01-01
    • 2021-02-28
    • 2021-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多