0

我有一张结构化的桌子,

列: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(收入)

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

4

1 回答 1

0

看来您不能在 IN 子句中使用带有 select 的枢轴。

在 sql fiddle 上对其进行了测试,您需要提供一个值列表以进行透视。也许您可以使用 pl/sql 函数将结果列转换为逗号分隔的列表,但不确定这是否可以被数据透视表使用。

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') )
于 2013-04-27T11:55:08.147 回答