首页 > 数据库 > mysql教程 > sql月份汇总查询语句

sql月份汇总查询语句

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
发布: 2016-06-07 17:47:34
原创
1390 人浏览过

sql月份汇总查询语句

select '铅笔' as [月份],
sum(case month(日期) when 1 then 铅笔 else 0 end) as [1月],
sum(case month(日期) when 2 then 铅笔 else 0 end) as [2月],
sum(case month(日期) when 3 then 铅笔 else 0 end) as [3月],
sum(case month(日期) when 4 then 铅笔 else 0 end) as [4月],
sum(case month(日期) when 5 then 铅笔 else 0 end) as [5月],
sum(铅笔) as 总价
from table

union all

select '钢笔' as [月份],
sum(case month(日期) when 1 then 钢笔 else 0 end) as [1月],
sum(case month(日期) when 2 then 钢笔 else 0 end) as [2月],
sum(case month(日期) when 3 then 钢笔 else 0 end) as [3月],
sum(case month(日期) when 4 then 钢笔 else 0 end) as [4月],
sum(case month(日期) when 5 then 钢笔 else 0 end) as [5月],
sum(钢笔) as 总价
from table


结果

 铅笔 1月 2月 3月 4月 5月 总价
  铅笔 200 500 600 700 500 2500
  钢笔 200 300 250 400 300 1450
  毛笔 300 400 500 300 600 2100
  总价 700 1200 1350 1400 1400 6050

相关标签:
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
php - prepare与sql的开销?
来自于 1970-01-01 08:00:00
0
0
0
sql文件
来自于 1970-01-01 08:00:00
0
0
0
打印sql语句
来自于 1970-01-01 08:00:00
0
0
0
sql优化or
来自于 1970-01-01 08:00:00
0
0
0
mysql - sql报错原因?
来自于 1970-01-01 08:00:00
0
0
0
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板