0

当前查询 -

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
4

2 回答 2

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;
于 2014-01-28T19:27:59.550 回答
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 |
于 2014-01-29T04:32:03.053 回答