项目中很多报表需要把表里的竖行变成横列.
table TA
consumer_code STORE SALE_AMOUNT
1 A 100
2 A 200
1 B 500
SQL可以这样:
SELECT consumer_code,
sum(decode(store,'A', SALE_AMOUNT,0)) as storeA,
sum(decode(store,'B', SALE_AMOUNT,0)) as storeB
FROM TA
group by consumer_code
显示效果:
consumer_code storeA storeB
1 100 500
2 200 0
此方法实用于 store数量比较少,可以列举的case.