浏览 8668 次
锁定老帖子 主题:sql按日期范围汇总数据
精华帖 (0) :: 良好帖 (0) :: 新手帖 (3) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2009-07-02
只想得到日期或时间的一部分,而不是完整的日期和时间。然后在统计。 用到的数据库和表(MS SqlServer数据库) use master go if exists(select * from sysdatabases where name='Spending') drop database Spending create database Spending on ( name='Spending_data', filename='D:\Spending_data.mdf', size=3 mb, filegrowth=10% ) log on ( name='Spending_log', filename='D:\Spending_log.ldf', size=3 mb, filegrowth=10% ) go use Spending go if exists(select * from sysobjects where name='consumption') drop table consumption go create table consumption ( cid int primary key identity(1,1), cdate datetime not null, cmoney money not null ) go insert into consumption values('20090624',23) insert into consumption values('20090625',56) insert into consumption values('20090625',42) insert into consumption values('20090626',45) insert into consumption values('20090627',12) insert into consumption values('20090628',32) insert into consumption values('20090629',36) insert into consumption values('20090701',16) select * from consumption 比如我想统计每天的数据和(25号时候有两条记录)用下面语句,只统计6月份每一天的数据和(如果年份多,再加上限制条件) select DATEPART(dd,cdate) as '号', sum(cmoney) from consumption where datepart(mm,cdate)=6 group by DATEPART(dd,cdate) 显示结果 24 23.00 25 98.00 26 45.00 、、、 统计一周的如下 select DATEPART(wk,cdate) as '周', sum(cmoney) from consumption group by DATEPART(wk,cdate) 显示结果 26 178.00 27 84.00 统计月的如下 select datepart(mm,cdate) as '月份',sum(cmoney) from consumption where datepart(yy,cdate)=2009 group by datepart(mm,cdate) 显示结果 6 246.00 7 16.00 以上用到的就是“DATEPART()”函数。函数DATEPART()的参数是两个变量。第一个变量指定要抽取日期的哪一部分;第二个变量是实际的数据。 日期的各部分及其简写 日期部分 简写 值 year yy 1753--9999 quarter qq 1--4 month mm 1--12 day of year dy 1--366 day dd 1--31 week wk 1--53 weekday dw 1--7(Sunday--Saturday) hour hh 0--23 minute mi 0--59 second ss 0--59 milisecond ms 0--999 再有就是我们数据库某字段设计为 datetime类型后,插入数据后,自动将毫秒数都显示出来了 例如我上面插入的是 20090624,可是在查出来就变成2009-06-24 00:00:00.000,这样在页面 显示肯定有问题,这种情况可以用CONVERT()函数 CONVERT(data_type,expression,[style]) 此样式一般在时间类型(datetime,smalldatetime)与字符串类型(nchar,nvarchar,char,varchar) 相互转换的时候才用到. 语句及查询结果: SELECT CONVERT(varchar(100), cdate, 0):24 06 2009 21:57PM SELECT CONVERT(varchar(100), cdate, 1): 06/24/09 SELECT CONVERT(varchar(100), cdate, 2): 09.06.24 SELECT CONVERT(varchar(100), cdate, 3): 24/06/09 SELECT CONVERT(varchar(100), cdate, 4): 24.06.09 SELECT CONVERT(varchar(100), cdate, 5): 24-06-09 SELECT CONVERT(varchar(100), cdate, 6): 24 06 09 SELECT CONVERT(varchar(100), cdate, 7): 06 24, 09 SELECT CONVERT(varchar(100), cdate,: 10:57:46 SELECT CONVERT(varchar(100), cdate, 9): 06 24 2009 21:57:46:827AM SELECT CONVERT(varchar(100), cdate, 10): 06-24-09 SELECT CONVERT(varchar(100), cdate, 11): 09/06/24 SELECT CONVERT(varchar(100), cdate, 12): 090624 SELECT CONVERT(varchar(100), cdate, 13): 24 06 2009 21:57:46:937 SELECT CONVERT(varchar(100), cdate, 14): 10:57:46:967 SELECT CONVERT(varchar(100), cdate, 20): 2009-06-24 21:57:47 SELECT CONVERT(varchar(100), cdate, 21): 2009-06-24 21:57:47.157 SELECT CONVERT(varchar(100), cdate, 22): 06/24/09 21:57:47 PM SELECT CONVERT(varchar(100), cdate, 23): 2009-06-24 SELECT CONVERT(varchar(100), cdate, 24): 10:57:47 SELECT CONVERT(varchar(100), cdate, 25): 2009-06-24 21:57:47.250 SELECT CONVERT(varchar(100), cdate, 100): 06 24 2009 21:57PM SELECT CONVERT(varchar(100), cdate, 101): 06/24/2009 SELECT CONVERT(varchar(100), cdate, 102): 2009.06.24 SELECT CONVERT(varchar(100), cdate, 103): 24/06/2009 SELECT CONVERT(varchar(100), cdate, 104): 24.06.2009 SELECT CONVERT(varchar(100), cdate, 105): 24-06-2009 SELECT CONVERT(varchar(100), cdate, 106): 24 06 2009 SELECT CONVERT(varchar(100), cdate, 107): 06 24, 2009 SELECT CONVERT(varchar(100), cdate, 108): 10:57:49 SELECT CONVERT(varchar(100), cdate, 109): 06 24 2009 21:57:49:437PM SELECT CONVERT(varchar(100), cdate, 110): 06-24-2009 SELECT CONVERT(varchar(100), cdate, 111): 2009/06/24 SELECT CONVERT(varchar(100), cdate, 112): 20090624 SELECT CONVERT(varchar(100), cdate, 113): 24 06 2009 21:57:49:513 SELECT CONVERT(varchar(100), cdate, 114): 10:57:49:547 SELECT CONVERT(varchar(100), cdate, 120): 2009-06-24 21:57:49 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |