mysql语句优化
来源:互联网 发布:淘宝宝贝所在地怎么改 编辑:程序博客网 时间:2024/06/06 07:14
这是年中详细月查询
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.idLEFT JOIN erp$base$customer_category f ON k.customer_category_id =f.id LEFT JOIN erp$base$area a ON c.area_id=a.idwhere DATE_FORMAT(c.orderDate,'%Y')=2015UNION 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.idLEFT JOIN erp$base$customer_category f ON k.customer_category_id =f.id LEFT JOIN erp$base$area a ON c.area_id=a.idwhere 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.idLEFT JOIN erp$base$customer_category f ON k.customer_category_id =f.id LEFT JOIN erp$base$area a ON c.area_id=a.idwhere (DATE_FORMAT(c.orderDate,'%Y')=2015 OR DATE_FORMAT(c.orderDate,'%Y')=2014) GROUP BY DATE_FORMAT(c.orderDate,'%Y');
0 0
- mysql语句优化
- mysql语句优化原则
- mysql 语句优化查询
- mysql语句优化
- mysql limit语句优化
- MySql语句优化原则
- Mysql 语句优化
- MySQL语句优化
- MySQL语句的优化
- mysql优化-----dml语句
- mysql优化-----ddl语句
- mysql查询语句优化
- Mysql sql语句优化
- mysql语句优化
- mysql sql语句优化
- mysql语句优化
- MYSQL查询语句优化
- mysql优化sql语句
- oracle parallel
- MySQL数据库命令行常用命令大全总结篇
- dll劫持
- Spark应用程序运行的日志存在哪里
- [漂浮法]POJ2528 Mayor's posters
- mysql语句优化
- [征稿啦]2015 IEEE先进信息技术、电子与自动化控制会议 (2015 IAEAC)
- bzoj 1588 splay入门题 (插入,查询)
- JSP的执行过程\JSP的隐式对象
- [LeetCode][Java] Search Insert Position
- linux shell route命令
- weak/strong创建控件的区别
- Robot Framework Advanced Guider from twiki.cern.ch
- perf