sql一些时间操作的技巧

上传人:xiao****1972 文档编号:84087453 上传时间:2019-03-02 格式:DOC 页数:5 大小:48KB
返回 下载 相关 举报
sql一些时间操作的技巧_第1页
第1页 / 共5页
sql一些时间操作的技巧_第2页
第2页 / 共5页
sql一些时间操作的技巧_第3页
第3页 / 共5页
sql一些时间操作的技巧_第4页
第4页 / 共5页
sql一些时间操作的技巧_第5页
第5页 / 共5页
亲,该文档总共5页,全部预览完了,如果喜欢就下载吧!
资源描述

《sql一些时间操作的技巧》由会员分享,可在线阅读,更多相关《sql一些时间操作的技巧(5页珍藏版)》请在金锄头文库上搜索。

1、SQL一些时间操作的技巧SQL datetime 如何取每小时记录数1. SELECT CONVERT(VARCHAR(10),DATE,120) AS 日期 ,DATEPART(hh,DATE) AS 小时,COUNT(*)AS 记录FROM Table1GROUP BY CONVERT(VARCHAR(10),DATE,120),DATEPART(hh,DATE)2. -取出小时,然后group by-group by left(convert(varchar(10),getdate(),108),2)每隔30s执行SP1.-创建一个存储过程(完成一分钟内的循环处理) create pro

2、c p_processes as declare d datetime set d=dateadd(minute,1,getdate() while d getdate() begin -你的更新处理 update . waitfor delay 00:00:20 -等待20秒后再继续 end go2能不能实现每隔30秒或20秒执行一次存储过程? begin WAITFOR DELAY 00:00:30 execute YourProc end 能不能在存储过程中从A表中取出数据,然后更新B表的数据 create proc YourProc as begin insert A select *

3、 from B endQ:我的数据表 A 中有很多数据,我需要取出的是每 1 分钟的数据,时间字段是 addtime 1 分钟到 2 分钟之间的数据为第2分钟的数据,需要取出的这个范围的最后一条数据Answer:1.-测试数据 declare t table ( id int identity(1,1) , price real, time datetime ) insert into t ( price, time ) select 1.0, 2004-08-23 16:40:00 union all select 1.1, 2004-08-23 16:40:05 union all sel

4、ect 1.2, 2004-08-23 16:40:08 union all select 1.6, 2004-08-23 16:41:00 union all select 1.7, 2004-08-23 16:45:00 union all select 1.5, 2004-08-23 16:45:05 union all select 1.3, 2004-08-23 16:46:00 union all select 1.1, 2004-08-23 16:49:00 -查询 select price,convert(varchar(16),time,121) time from t wh

5、ere id in ( select max(id) from t group by convert( varchar(16), time, 121 ) ) order by id 结果: price time - - 1.2 2004-08-23 16:40 1.6 2004-08-23 16:41 1.5 2004-08-23 16:45 1.3 2004-08-23 16:46 1.1 2004-08-23 16:49 得到这样的结果就够了,其它的你应该在绘图的程序中来完成,否则得不偿失2. select sum(a) as mycount from table1 group by le

6、ft(addtime,12)Q: 现在给出一个时间段,得到了这个时间段的总和,但是怎么遍历这个时间段中每一天的数据和呢!之后算出每一天的和占总和的比例Answer:select convert(varchar(10),时间,120), sum(col), (select sum(col) from tb), ltrim(cast(sum(col)*100.0/(select sum(col) from tb) as dec(18,2)+%from tbgroup by convert(varchar(10),时间,120)SQL统计上周、昨天等数据DATEPART应用统计昨天的数据:selec

7、t XXXXXXX from XXXXXwhere DATEPART(dd, addtime) = DATEPART(dd, GETDATE()-1 and DATEPART(mm, addtime) = DATEPART(mm, GETDATE() and DATEPART(yy, addtime) = DATEPART(yy, GETDATE()统计上周的数据:select XXXXXXX from XXXXXwhereDATEPART(ww, addtime) = DATEPART(ww, GETDATE()-1 and DATEPART(yy, addtime) = DATEPART(

8、yy, GETDATE()统计上月的数据:select XXXXXXX from XXXXXwhereDATEPART(mm, addtime) = DATEPART(mm, GETDATE()-1 and DATEPART(yy, addtime) = DATEPART(yy, GETDATE()关键是DATEPART函数的应用,具体可以查询SQL DATEPART用法。假设: 有个用户注册表user,有注册时间create_day字段,我想查询本周注册人数,上周注册人数,本月注册人数,上月注册人数,分别怎么写sql语句 -查询本周注册人数 select count(*) from user

9、 where datediff(week,create_day-1,getdate()=0 -上周注册人数 select count(*) from user where datediff(week,create_day-1,getdate()=1 -本月注册人数 select count(*) from user where datediff(month,create_day,getdate()=0 -上月注册人数 select count(*) from user where datediff(month,create_day,getdate()=1 . -本周 select count(

10、*) from user where datediff(dd,create_day,getdate() = datepart(dw,getdate() -上周 select count(*) from user where datediff(dd,create_day,(getdate() - datepart(dw,getdate() =0 and DATEDIFF(d,date1,GETDATE()5;-获取本月数据select * from dates where DATEDIFF(mm,date1,GETDATE()=0;-获取本月数据上一月的将0改为1-获取上一年数据select * from dates where DATEDIFF(yyyy,date1,GETDAT

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 大杂烩/其它

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号