mysql语句优化

这是年中详细月查询

select DATE_FORMAT(c.orderDate,'%Y') as '年份',
sum( case  DATE_FORMAT(c.orderDate,'%m')  when 1  then c.totalAmount else  0 end  ) as '一月',
sum( case  DATE_FORMAT(c.orderDate,'%m')  when 2  then c.totalAmount else  0 end  ) as '二月',
sum( case  DATE_FORMAT(c.orderDate,'%m')  when 3  then c.totalAmount else  0 end  ) as '三月',
sum( case  DATE_FORMAT(c.orderDate,'%m')  when 4  then c.totalAmount else  0 end  ) as '四月',
sum( case  DATE_FORMAT(c.orderDate,'%m')  when 5  then c.totalAmount else  0 end  ) as '五月',
sum( case  DATE_FORMAT(c.orderDate,'%m')  when 6  then c.totalAmount else  0 end  ) as '六月',
sum( case  DATE_FORMAT(c.orderDate,'%m')  when 7  then c.totalAmount else  0 end  ) as '七月',
sum( case  DATE_FORMAT(c.orderDate,'%m')  when 8  then c.totalAmount else  0 end  ) as '八月',
sum( case  DATE_FORMAT(c.orderDate,'%m')  when 9  then c.totalAmount else  0 end  ) as '九月',
sum( case  DATE_FORMAT(c.orderDate,'%m')  when 10  then c.totalAmount else  0 end  ) as '十月',
sum( case  DATE_FORMAT(c.orderDate,'%m')  when 11  then c.totalAmount else  0 end  ) as '十一月',
sum( case  DATE_FORMAT(c.orderDate,'%m')  when 12  then c.totalAmount else  0 end  ) as '十二月',
sum(c.totalAmount) as '总金额' 
FROM erp$sales$sales_order c 
LEFT JOIN erp$base$customer k ON c.customer_id=k.id
LEFT JOIN erp$base$customer_category f  ON  k.customer_category_id =f.id 
LEFT JOIN erp$base$area a ON  c.area_id=a.id
where DATE_FORMAT(c.orderDate,'%Y')=2015
UNION ALL select DATE_FORMAT(c.orderDate,'%Y') as '年份',
sum( case  DATE_FORMAT(c.orderDate,'%m')  when 1  then c.totalAmount else  0 end  ) as '一月',
sum( case  DATE_FORMAT(c.orderDate,'%m')  when 2  then c.totalAmount else  0 end  ) as '二月',
sum( case  DATE_FORMAT(c.orderDate,'%m')  when 3  then c.totalAmount else  0 end  ) as '三月',
sum( case  DATE_FORMAT(c.orderDate,'%m')  when 4  then c.totalAmount else  0 end  ) as '四月',
sum( case  DATE_FORMAT(c.orderDate,'%m')  when 5  then c.totalAmount else  0 end  ) as '五月',
sum( case  DATE_FORMAT(c.orderDate,'%m')  when 6  then c.totalAmount else  0 end  ) as '六月',
sum( case  DATE_FORMAT(c.orderDate,'%m')  when 7  then c.totalAmount else  0 end  ) as '七月',
sum( case  DATE_FORMAT(c.orderDate,'%m')  when 8  then c.totalAmount else  0 end  ) as '八月',
sum( case  DATE_FORMAT(c.orderDate,'%m')  when 9  then c.totalAmount else  0 end  ) as '九月',
sum( case  DATE_FORMAT(c.orderDate,'%m')  when 10  then c.totalAmount else  0 end  ) as '十月',
sum( case  DATE_FORMAT(c.orderDate,'%m')  when 11  then c.totalAmount else  0 end  ) as '十一月',
sum( case  DATE_FORMAT(c.orderDate,'%m')  when 12  then c.totalAmount else  0 end  ) as '十二月',
sum(c.totalAmount) as '总金额' 
FROM erp$sales$sales_order c 
LEFT JOIN erp$base$customer k ON c.customer_id=k.id
LEFT JOIN erp$base$customer_category f  ON  k.customer_category_id =f.id 
LEFT JOIN erp$base$area a ON  c.area_id=a.id
where DATE_FORMAT(c.orderDate,'%Y')=2014

还有别一种更好

select DATE_FORMAT(c.orderDate,'%Y') as '年份',
sum( case  DATE_FORMAT(c.orderDate,'%m')  when 1  then c.totalAmount else  0 end  ) as '一月',
sum( case  DATE_FORMAT(c.orderDate,'%m')  when 2  then c.totalAmount else  0 end  ) as '二月',
sum( case  DATE_FORMAT(c.orderDate,'%m')  when 3  then c.totalAmount else  0 end  ) as '三月',
sum( case  DATE_FORMAT(c.orderDate,'%m')  when 4  then c.totalAmount else  0 end  ) as '四月',
sum( case  DATE_FORMAT(c.orderDate,'%m')  when 5  then c.totalAmount else  0 end  ) as '五月',
sum( case  DATE_FORMAT(c.orderDate,'%m')  when 6  then c.totalAmount else  0 end  ) as '六月',
sum( case  DATE_FORMAT(c.orderDate,'%m')  when 7  then c.totalAmount else  0 end  ) as '七月',
sum( case  DATE_FORMAT(c.orderDate,'%m')  when 8  then c.totalAmount else  0 end  ) as '八月',
sum( case  DATE_FORMAT(c.orderDate,'%m')  when 9  then c.totalAmount else  0 end  ) as '九月',
sum( case  DATE_FORMAT(c.orderDate,'%m')  when 10  then c.totalAmount else  0 end  ) as '十月',
sum( case  DATE_FORMAT(c.orderDate,'%m')  when 11  then c.totalAmount else  0 end  ) as '十一月',
sum( case  DATE_FORMAT(c.orderDate,'%m')  when 12  then c.totalAmount else  0 end  ) as '十二月',
sum(c.totalAmount) as '总金额' 
FROM erp$sales$sales_order c 
LEFT JOIN erp$base$customer k ON c.customer_id=k.id
LEFT JOIN erp$base$customer_category f  ON  k.customer_category_id =f.id 
LEFT JOIN erp$base$area a ON  c.area_id=a.id
where (DATE_FORMAT(c.orderDate,'%Y')=2015 OR DATE_FORMAT(c.orderDate,'%Y')=2014) GROUP BY DATE_FORMAT(c.orderDate,'%Y');
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值