将pivot列中的XML列数据显示为单个列

时间:2013-04-26 21:47:41

标签: sql xml oracle pivot

我有一个结构化的表,

  

列:month_yyyy,account_id,account_name,收入

我想在表格中转动数据,使其显示为

  

列:account_id,account_name,month_yyyy1(收入),month_yyyy2(收入)... monthyyyy_n(收入)

我这样做了,

    select * from ( 
SELECT month_yyyy,
       account_id,
       account_name,
       revenue
  FROM table
   order by to_date(month_yyyy, 'month yyyy')
)
pivot xml(
    sum (revenue) as revenue for (month_yyyy) in (select distinct month_yyyy from table) 
  ) 
order by account_name;

这提供了与旋转的xml列相似的数据

<PivotSet><item><column name = "MONTH_YYYY">April 2012</column><column name = "REVENUE">0</column></item><item><column name = "MONTH_YYYY">August 2012</column><column name = "REVENUE">0</column></item><item><column name = "MONTH_YYYY">December 2012</column><column name = "REVENUE">0</column></item><item><column name = "MONTH_YYYY">Decmber 2011</column><column name = "REVENUE">0</column></item><item><column name = "MONTH_YYYY">February 2012</column><column name = "REVENUE">0</column></item><item><column name = "MONTH_YYYY">February 2013</column><column name = "REVENUE">0</column></item><item><column name = "MONTH_YYYY">January 2012</column><column name = "REVENUE">0</column></item><item><column name = "MONTH_YYYY">January 2013</column><column name = "REVENUE">0</column></item><item><column name = "MONTH_YYYY">July 2012</column><column name = "REVENUE">0</column></item><item><column name = "MONTH_YYYY">June 2012</column><column name = "REVENUE">0</column></item><item><column name = "MONTH_YYYY">March 2012</column><column name = "REVENUE">0</column></item><item><column name = "MONTH_YYYY">March 2013</column><column name = "REVENUE">0</column></item><item><column name = "MONTH_YYYY">May 2012</column><column name = "REVENUE">0</column></item><item><column name = "MONTH_YYYY">November 2011</column><column name = "REVENUE">0</column></item><item><column name = "MONTH_YYYY">November 2012</column><column name = "REVENUE">0</column></item><item><column name = "MONTH_YYYY">October 2012</column><column name = "REVENUE">0</column></item><item><column name = "MONTH_YYYY">September 2012</column><column name = "REVENUE">0</column></item></PivotSet>

如何创建一个视图,按月选择并显示xml列数据作为单个列?我想实现,

  

account_id,account_name,month_yyyy1(收入),month_yyyy2(收入)... monthyyyy_n(收入)

感谢您提供的任何帮助,如果需要进一步的信息,请告诉我。

1 个答案:

答案 0 :(得分:0)

看来你不能在IN子句中使用带有select的数据。

在sql小提琴上的

I tested it,你需要提供一个值的列表来转动。也许您可以使用pl / sql函数将结果列转换为以逗号分隔的列表,但不确定它是否可以由pivot使用。

create table tab
( month_yyyy varchar2(20)
, account_id number
, account_name varchar2(20)
, revenue number
);

insert into tab values ('januari_2013', 1, 'acc one', 10);
insert into tab values ('januari_2013', 2, 'acc two', 20);
insert into tab values ('februari_2013', 1, 'acc one', 100);
insert into tab values ('februari_2013', 2, 'acc two', 200);
insert into tab values ('march_2013', 1, 'acc one', 1000);
insert into tab values ('march_2013', 2, 'acc two', 2000);

select *
from   tab
-- this does not work: pivot ( sum(revenue) FOR month_yyyy IN (select distinct month_yyyy from tab) )
-- this works:
pivot ( sum(revenue) FOR month_yyyy IN ('januari_2013', 'februari_2013', 'march_2013') )