请教一条sql的写法
现在通过操作我已经得到了一个如下的集合
A type value
a 1 v1
b 2 v2
c 1 v3
d 2 v4
我如何才能最高效的构造成下面的集合
A value1 value2
a v1
b v2
c v3
d v4
即type为1时将value写入value1,type为2时将value写入value2
我的想法如下:
select A, value, t1.value
from TB where type=1
left join TB t1 on TB.A=t1.A
having type=2
结果不行啊
------解决方案--------------------select
A,
case when type=1 then value else null end as value1,
case when type=2 then value else null end as value2
from
TB
或者
select
A,
decode(type,1,value,null) as value1,
decode(type,2,value,null) as value2
from
TB
------解决方案--------------------select A,
sum(decode(type,1,value,null),
sum(decode(type,2,value,null)
from tb
group by A
------解决方案--------------------select decode(type,1, value,null) as value1,decode(type,2,value,null) as value2 from TB
如果对了,要请吃饭的