id qty stuff_id re
P001 100 S00001
P001 200 S00002
P002 3000 S00001
P002 2000 S00002
P003 100 S00001
P003 200 S00002
P003 300 S00003
展开如下:
id S0001 S0002 S0003
P001 100 200 0
P002 3000 2000 0
P003 100 200 300请大家给点提示?
P001 100 S00001
P001 200 S00002
P002 3000 S00001
P002 2000 S00002
P003 100 S00001
P003 200 S00002
P003 300 S00003
展开如下:
id S0001 S0002 S0003
P001 100 200 0
P002 3000 2000 0
P003 100 200 300请大家给点提示?
http://community.csdn.net/Expert/topic/5161/5161578.xml?temp=.2244379
sum(case where stuff_id ='S00001' then qty else 0 end) as S00001,
sum(case where stuff_id ='S00002' then qty else 0 end) as S00001,
sum(case where stuff_id ='S00003' then qty else 0 end) as S00001
from tb
group by id
就使用 case when