读书人

求SQL话语转换

发布时间: 2012-12-14 10:33:07 作者: rapoo

求SQL语句转换
本帖最后由 rabocn 于 2012-11-14 14:52:01 编辑 表A
item value
AD_AB 1.2
AD_BC 1.3
B_AB 1.6
B_BC 1.7
现在希望得到的结果

type AB BC
AD 1.2 1.3
B 1.6 1.7


求大神指教,谢谢
[最优解释]
select
substr(item, 1, instr(item, '_')-1) type,
substr(item, instr(item, '_')+1) item,
value from A;
[其他解释]

select regexp_substr(item,'[^_]+',1,1) type,
sum(decode(regexp_substr(item,'[^_]+',1,2),'AB',value,0)) ab,
sum(decode(regexp_substr(item,'[^_]+',1,2),'AC',value,0)) ac
from A group by regexp_substr(item,'[^_]+',1,1)

[其他解释]
select regexp_substr(item,'[^_]+',1,1) type,
sum(decode(regexp_substr(item,'[^_]+',1,2),'AB',value,0)) ab,
sum(decode(regexp_substr(item,'[^_]+',1,2),'BC',value,0)) bc
from A group by regexp_substr(item,'[^_]+',1,1)

[其他解释]
SELECT STR1 AS TYPE,
MAX(DECODE(STR2, 'AB', VALUE, 0)) AS AB,
MAX(DECODE(STR2, 'BC', VALUE, 0)) AS BC
FROM (SELECT REGEXP_SUBSTR(item, '[A-Z]+', 1, 1) AS STR1,
REGEXP_SUBSTR(item, '[A-Z]+', 1, 2) AS STR2,
value
from TEST)
GROUP BY STR1
[其他解释]
引用:
select
substr(item, 1, instr(item, '_')-1) type,
substr(item, instr(item, '_')+1) item,
value from A;


好像不对嘛

读书人网 >oracle

热点推荐