oracle中的数据透视查询

时间:2015-07-01 08:09:44

标签: sql oracle pivot

我有一个要求来转动数据。我使用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

我想显示如下数据..

enter image description here

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

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

2 个答案:

答案 0 :(得分:1)

你无法动态转动;需要在分析时知道值的数量(对应于最终结果集中的列数)。在pivot的IN子句中使用子查询是无效的语法。您需要对要查看的值进行硬编码。

可以使用动态XML数据透视图,但这可能不会对您有所帮助;你need to unpack the XML somehow。您也可能to use dynamic SQL to generate the query但是参考游标结果可能不适合您。

答案 1 :(得分:0)

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

根据我的理解

  

子查询仅与XML关键字一起使用。

相关问题