t1 销售类型(c1) 总金额(c2) 总数量(c3) 产品名(c4)
A 100 50 红
B 101 51 黄
C 102 52 绿
====> t2 c1 红 黄 绿
金额 100 101 102
数量 50 51 52
类型 A B C
如何转变??
oracle数据库
oracle行转列
答案:2 悬赏:30 手机版
解决时间 2021-12-20 21:33
- 提问者网友:轮囘Li巡影
- 2021-12-20 18:33
最佳答案
- 五星知识达人网友:洎扰庸人
- 2021-12-20 18:56
不知道合格不?
SELECt '金额' as c1,
To_char (sum(decode(品名, '红', 金额, null))) AS 红,
To_char (sum(decode(品名, '黄', 金额, null))) AS 黄,
To_char (sum(decode(品名, '绿', 金额, null))) AS 绿
FROM TEST
union all
SELECt '数量' as c1,
To_char (sum(decode(品名, '红', 数量, null))) AS 红,
To_char (sum(decode(品名, '黄', 数量, null))) AS 黄,
To_char (sum(decode(品名, '绿', 数量, null))) AS 绿
FROM TEST
union all
SELECt '类型' as c1,
replace (sum(replace (decode(品名, '红', 类型, null),'A',1)),1,'A') AS 红,
replace (sum(replace (decode(品名, '黄', 类型, null),'B',2)),2,'B') AS 黄,
replace (sum(replace (decode(品名, '绿', 类型, null),'C',3)),3,'C') AS 绿
FROM TEST
SELECt '金额' as c1,
To_char (sum(decode(品名, '红', 金额, null))) AS 红,
To_char (sum(decode(品名, '黄', 金额, null))) AS 黄,
To_char (sum(decode(品名, '绿', 金额, null))) AS 绿
FROM TEST
union all
SELECt '数量' as c1,
To_char (sum(decode(品名, '红', 数量, null))) AS 红,
To_char (sum(decode(品名, '黄', 数量, null))) AS 黄,
To_char (sum(decode(品名, '绿', 数量, null))) AS 绿
FROM TEST
union all
SELECt '类型' as c1,
replace (sum(replace (decode(品名, '红', 类型, null),'A',1)),1,'A') AS 红,
replace (sum(replace (decode(品名, '黄', 类型, null),'B',2)),2,'B') AS 黄,
replace (sum(replace (decode(品名, '绿', 类型, null),'C',3)),3,'C') AS 绿
FROM TEST
全部回答
- 1楼网友:深街酒徒
- 2021-12-20 19:38
测试表测了下,可以用,你看看,主要是先根据逗号进行分割,然后connect,level等于逗号的数目:
select id,nvl(substr(glbh,instr(glbh,',',1,lvl)+1,instr(glbh,',',1,lvl+1)-instr(glbh,',',1,lvl)-1),'kong') glbh
from (
select id,lvl,','||glbh||',' glbh
from test a ,
(select level lvl from dual connect by level<=
(select max(length(glbh)-length(replace(glbh,',')))+1 from test)) b
) t1
where substr(glbh,instr(glbh,',',1,lvl)+1,instr(glbh,',',1,lvl+1)-instr(glbh,',',1,lvl)-1) is not null
order by id,glbh;
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯