当前位置: 数据库>sqlserver
SQL语句技巧:按月统计数据
来源: 互联网 发布时间:2014-09-05
本文导语: 表结构如下: qty date ---------------------------------------------- 13 2005/01/17 15 2005/01/19 3 2005/01/25 105 2005/01/27 1 2005/01/31 352 2005/02/03 12 2005/02/04 255 2005/02/07 6 2005/02/18 1 2005/02/19 28 2005/02/21 1 2005/02/22 394 2005/02/23 359 2005/02/24 313 2005/02/...
表结构如下:
qty date
----------------------------------------------
13 2005/01/17
15 2005/01/19
3 2005/01/25
105 2005/01/27
1 2005/01/31
352 2005/02/03
12 2005/02/04
255 2005/02/07
6 2005/02/18
1 2005/02/19
28 2005/02/21
1 2005/02/22
394 2005/02/23
359 2005/02/24
313 2005/02/25
325 2005/02/26
544 2005/02/27
68 2005/02/28
2 2005/03/01
求一個SQL寫法,將每個月的數量求和,
如:
qry date
137 2005/01
?? 2005/02
?? 2005/03
SQL语句如下:
select sum(qty), datename(year, date) as year, datename(month, date) as month
from table
group by year, month
qty date
----------------------------------------------
13 2005/01/17
15 2005/01/19
3 2005/01/25
105 2005/01/27
1 2005/01/31
352 2005/02/03
12 2005/02/04
255 2005/02/07
6 2005/02/18
1 2005/02/19
28 2005/02/21
1 2005/02/22
394 2005/02/23
359 2005/02/24
313 2005/02/25
325 2005/02/26
544 2005/02/27
68 2005/02/28
2 2005/03/01
求一個SQL寫法,將每個月的數量求和,
如:
qry date
137 2005/01
?? 2005/02
?? 2005/03
SQL语句如下:
select sum(qty), datename(year, date) as year, datename(month, date) as month
from table
group by year, month