首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > oracle >

求SQL话语转换

2012-12-14 
求SQL语句转换本帖最后由 rabocn 于 2012-11-14 14:52:01 编辑表AitemvalueAD_AB1.2AD_BC1.3 B_AB1.6 B_BC

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


好像不对嘛

热点排行