sql 将纵列数据查询后为横向数据

已知品牌销售表 tb_sell:

字段:brand   //品牌   青岛啤酒,百威啤酒,雪花啤酒

:totalPrice  //销售总金额

: date  日期(日)

请用一条sql语句写出每天三种商品各自的销售量,效果日下:

日期             青岛           百威 雪花

2017-02-24  100           200 300

2017-02-25  150           200 300


方法1:

SELECT rq 日期,max(if(spmc='qd',xsje,0)) 青岛啤酒(金额),max(if(spmc='bw',xsje,0)) 百威啤酒(金额,max(if(spmc='sdl',xsje,0)) 三得利啤酒(金额)
 FROM `tbshell` GROUP BY  Rq;



方法2:
select rq 日期, max(case spmc when 'qd' then xsje else 0 end) 青岛啤酒(金额),
max(case spmc when 'bw' then xsje else 0 end) 百威啤酒(金额),
max(case spmc when 'sdl' then xsje else 0 end) 三得利啤酒(金额)
from tbshell group by rq;


注:使用max函数;

你可能感兴趣的:(sql)