我的不行的话就改一下 select 城市,max(数量0),max(数量0),max(金额0) from (select 城市,max(decode(类别,'0',数量,0)) 数量0,max(decode(类别,'1',数量,0)) 数量1,max(decode(类别,'0',金额,0)) 金额0 from 表1) group by 城市;
select t0.城市,t0.数量,t1.数量,t0.金额 from (select * from tbl where 类别='0') t0, (select * from tbl where 类别='1') t1 where t0.城市=t1.城市ok
select 城市,数量0,数量1,金额0 from (select 城市,max(decode(类别,'0',数量,0)) 数量0,max(decode(类别,'1',数量,0)) 数量1,max(decode(类别,'0',金额,0)) 金额0 from 表1 group by 城市);
select 城市,sum(decode(类别,'0',数量)) 数量(0), sum(decode(类别,'1',数量)) 数量(1), sum(decode(类别,'0',金额)) 金额(0) from 表1 group by 城市
select t0.城市,t0.数量,t1.数量,t0.金额
from (select * from tbl where 类别='0') t0,
(select * from tbl where 类别='1') t1
where t0.城市=t1.城市
dalian 2 1000 0
dalian 1 500 1
shenyang 3 500 0
shenyang 1 500 1表2 城市 数量(0) 数量(1) 金额(0)
dalian 2 1 1000
shenyang 3 1 500 其中 数量(0) 表示 类别是0的数量
数量(1) 表示 类别是1的数量
金额 (0) 表示 类别是0的金额
(即将表1中按照 类别的不同生成表2的结果集)
select 城市,max(数量0),max(数量0),max(金额0) from (select 城市,max(decode(类别,'0',数量,0)) 数量0,max(decode(类别,'1',数量,0)) 数量1,max(decode(类别,'0',金额,0)) 金额0 from 表1) group by 城市;
from (select * from tbl where 类别='0') t0,
(select * from tbl where 类别='1') t1
where t0.城市=t1.城市ok
sum(decode(类别,'1',数量)) 数量(1),
sum(decode(类别,'0',金额)) 金额(0)
from 表1
group by 城市