求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