2014-01-28 42 views

回答

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; 
0

除了什麼戈登建議,如果你正在使用11克版本,你可以使用旋轉如下

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 |