分享一个sql (主要体现case when 作用)

来源:互联网 发布:淘宝零食怎么那么便宜 编辑:程序博客网 时间:2024/06/14 23:10
select tt.ymd,sum(tt.`在线支付 - 现金支付`) `在线支付 - 现金支付`,sum(tt.`在线支付 - 云融余额支付`) `在线支付 - 云融余额支付`,sum(tt.`在线支付 - pos机支付`) `在线支付 - pos机支付`,sum(tt.`在线支付 - 消费券支付`) `在线支付 - 消费券支付`,sum(tt.`在线支付 - 通联支付`) `在线支付 - 通联支付`,sum(tt.`在线支付`) `在线支付`,sum(tt.`线下支付 - pos机支付`) `线下支付 - pos机支付`,sum(tt.`线下支付 - 云融余额支付`) `线下支付 - 云融余额支付`,sum(tt.`在线支付 - 微信支付`) `在线支付 - 微信支付`,sum(tt.`线下支付 - 消费券支付`) `线下支付 - 消费券支付`,sum(tt.`线下支付`) `线下支付`from (select t.ymd,case when t.method='在线支付 - 现金支付' then t.amt else 0 end `在线支付 - 现金支付`,case when t.method='在线支付 - 云融余额支付' then t.amt else 0 end `在线支付 - 云融余额支付`,case when t.method='在线支付 - pos机支付' then t.amt else 0 end `在线支付 - pos机支付`,case when t.method='在线支付 - 消费券支付' then t.amt else 0 end `在线支付 - 消费券支付`,case when t.method='在线支付 - 通联支付' then t.amt else 0 end `在线支付 - 通联支付`,case when t.method='在线支付' then t.amt else 0 end `在线支付`,case when t.method='线下支付 - pos机支付' then t.amt else 0 end `线下支付 - pos机支付`,case when t.method='线下支付 - 云融余额支付' then t.amt else 0 end `线下支付 - 云融余额支付`,case when t.method='在线支付 - 微信支付' then t.amt else 0 end `在线支付 - 微信支付`,case when t.method='线下支付 - 消费券支付' then t.amt else 0 end `线下支付 - 消费券支付`,case when t.method='线下支付' then t.amt else 0 end `线下支付`from (SELECTa.payment_method method,SUM(a.amount) amt,b.shop_info,DATE_FORMAT(a.create_date,'%Y-%m-%d') ymdFROMxx_payment aLEFT JOIN xx_order b ON a.orders = b.idWHEREb.order_source = 3AND b.shop_info = '253'  AND DATE_FORMAT(a.create_date,'%Y-%m-%d')>= DATE_FORMAT('2015-6-1','%Y-%m-%d')   AND DATE_FORMAT(a.create_date,'%Y-%m-%d')<= DATE_FORMAT('2015-6-30' ,'%Y-%m-%d')  -- AND DATE_FORMAT(a.create_date,'%Y-%m-%d')>= DATE_FORMAT(CURDATE(),'%Y-%m-01')   -- AND DATE_FORMAT(a.create_date,'%Y-%m-%d')<= DATE_FORMAT(CURDATE() ,'%Y-%m-%d')  GROUP BY DATE_FORMAT(a.create_date,'%Y-%m-%d') ,a.payment_method) t) tt group by tt.ymd

0 0
原创粉丝点击