日期:2014-05-17 浏览次数:20475 次
select 1 as 编号 ,2.3 as 价格,'A' as 类型 union ALL
select 1 ,1.2,'B' union ALL
select 2 ,3.8,'B' union ALL
select 3 ,5.9,'A' union ALL
select 4 ,1.3,'A' union ALL
select 4 ,4.6,'B'
;with maco as
(
select 1 as 编号 ,2.3 as 价格,'A' as 类型 union ALL
select 1 ,1.2,'B' union ALL
select 2 ,3.8,'B' union ALL
select 3 ,5.9,'A' union ALL
select 4 ,1.3,'A' union ALL
select 4 ,4.6,'B'
)
select
编号,'A' as 类型,
sum(case WHEN 类型='A' Then 价格 else 0 end) as 价格,
'B' as 类型,
sum(case WHEN 类型='B' Then 价格 else 0 end) as 价格
from maco group by 编号
/*
编号 类型 价格 类型 价格
----------- ---- --------------------------------------- ---- ---------------------------------------
1 A 2.3 B 1.2
2 A 0.0 B 3.8