Convert Pivot xml output to tabular output without xml

空扰寡人 提交于 2019-12-11 04:08:17

问题


I have following kind of table:

 ID | Key | Value
 --   ---   -----
 1     A     aa    
 2     B     bb
 3     A     ay
 4     C     cc
 5     B     bx
 6     C     ct 

I need the output :

A    B    C
---  ---  ---
aa   bb   cc
ay   bx   ct

When I use PIVOT with subquery it does not work:

Select * from (Select Key, Value, Id from tableName
pivot (max(Value) for Key IN (SELECT distinct Key from tableName)));

If I use PIVOT xml, the subquery works:

Select * from (Select Key, Value, Id from tableName)
pivot xml (max(Value) for Key IN (SELECT distinct Key from tableName));

but when using PIVOT xml I get the output in xml form:

 A                      B    C
---                    ---  ---
<PivotSet><item...aa   ...   ...

How can this be converted to tabular output without the xml tags? Any help will be appreciated. Thanks!

来源:https://stackoverflow.com/questions/42851887/convert-pivot-xml-output-to-tabular-output-without-xml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!