当前查询 -
Select Item, Attribute, AttributeValue from table;
Item Attribute Attributevalue
1 Color Brown
1 Width 24
1 Height 36
2 color white
2 Width 10
2 height 15
我正在尝试将输出设为:
Item Color Width Height
1 brown 24 36
2 white 10 15
答案 0 :(得分:3)
这不是字符串聚合,而是旋转。尽管最新版本的Oracle支持pivot
关键字,但您也可以使用聚合执行此操作:
select item,
max(case when attribute = 'Color' then Attributevalue end) as color,
max(case when attribute = 'Width' then Attributevalue end) as Width,
max(case when attribute = 'Height' then Attributevalue end) as Height
from table t
group by item;
答案 1 :(得分:0)
除了Gordon建议的内容,如果你使用的是11g版本,你可以使用pivot如下
with tab(Item,Attribute,Attributevalue) as
(select 1,'Color','Brown' from dual union all
select 1,'Width','24' from dual union all
select 1,'Height','36' from dual union all
select 2,'Color','white' from dual union all
select 2,'Width','10' from dual union all
select 2,'Height','15' from dual)
------
--end of data preparation
------
select *
from tab
pivot (max(ATTRIBUTEVALUE) for ATTRIBUTE in ('Color' as color,
'Width' as width,
'Height' as height));
输出:
| ITEM | COLOR | WIDTH | HEIGHT |
|------|-------|-------|--------|
| 1 | Brown | 24 | 36 |
| 2 | white | 10 | 15 |