【问题标题】:pivot query in oracleoracle中的pivot查询
【发布时间】:2015-09-18 06:57:52
【问题描述】:

我有一个数据透视的要求。我已经尝试过使用 pivot 进行以下查询:

select * from 

(
  select g.geography_code,
       g.description,
       a.external_id_1 as ext
from geography_alignment_timeline gal
join geography g on g.geography_id=gal.geography_id
join alignment a on gal.alignment_id=a.alignment_id
join team t on a.team_id=t.team_id
where g.tenant_id=500104
and   g.region_id=7888500001071037
and   t.team_id=70000012130101
)
pivot
(
  max(ext) for ext in (select external_id_1 from alignment where tenant_id=500104)
);

我得到了错误:

ORA-00936: missing expression

 Tables and columns: 
     Geography : Geography_id,Geography_code,Description,External_id_1
     Geography_alignment_timeline : Geography_id,Alignment_id,External_id_1
     Alignment : Alignment_id,team_id,Alignment_name,External_id_1
     team : Team_id,name,Description,External_id_1

我想显示如下数据..

每个团队都有单独的 alignment_ids。我想显示上面的数据。

你能帮我解决一下问题吗?

【问题讨论】:

  • 我们没有您的表格,我们没有您的数据,请问有人可以帮忙吗?请发布一个测试用例。
  • sqlfiddle.com/#!4 或者您可以发布完整的错误消息。内部查询是否独立工作?
  • 您不能在 IN 子句中包含子查询。它应该是hard coded 值。

标签: sql oracle pivot


【解决方案1】:

阅读与pivot子句相关的this文档。

据我了解

子查询仅与 XML 关键字结合使用。

【讨论】:

    【解决方案2】:

    您不能动态旋转;在解析时需要知道值的数量(对应于最终结果集中的列数)。在枢轴的IN 子句中使用子查询是无效的语法。您需要对期望看到的值进行硬编码。

    可以有一个动态的 XML 轴,但这在这里可能对您没有帮助;你会need to unpack the XML somehow。您也可以to use dynamic SQL to generate the query,但引用游标结果可能不适合您。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-30
      • 2016-01-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-17
      • 1970-01-01
      相关资源
      最近更新 更多