我有一张结构化的桌子,
列: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(收入)
感谢您提供的任何帮助,如果需要更多信息,请告诉我。