当前位置: 首页 > 图文教程 > 数据库 > MSSQL > SQL语句技巧:按月统计数据

MSSQL
SQL Server数据库技术(36)
SQL Server数据库技术(37)
SQL Server数据库技术(41)
SQL Server数据库技术(42)
SQL Server数据库技术(43)
SQL Server数据库技术(44)
SQL Server数据库技术(45)
SQL Server数据库技术(46)
SQL Server数据库技术(47)
SQL Server数据库技术(48)
SQL Server数据库技术(49)
SQL Server数据库技术(50)
SQL Server数据库技术(51)
SQL Server数据库技术(53)
SQL Server数据库技术(54)
SQL Server数据库技术(55)
SQL Server数据库技术(56)
SQL Server数据库技术(57)
SQL Server数据库技术(58)
SQL Server数据库技术(59)

MSSQL 中的 SQL语句技巧:按月统计数据


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-13   浏览: 115 ::
收藏到网摘: n/a

表结构如下:
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