博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SQL 查询间隔时间大于60s的所有数据
阅读量:7129 次
发布时间:2019-06-28

本文共 1117 字,大约阅读时间需要 3 分钟。

 CREATE TABLE test
(
      id int PRIMARY KEY IDENTITY(1,1),
      dealerId int,
      actiontime datetime,
      actiontype int
)
 
 
insert into dbo.Test
select '1','2013-6-24 12:30:00',1
union all
select '1','2013-6-24 12:30:30',1
union all
select '1','2013-6-24 12:31:00',1
union all
select '1','2013-6-24 12:32:00',1
 
--select * from dbo.Test
--创建临时表
if object_id('tempdb..#temp') is not null
Begin
    drop table #temp
   --select * from #temp
End
create table #temp
(
      rownum int primary key,
      id int ,
      dealerid nvarchar(10),
      actiontime datetime,
      actiontype int   
)
--读入数据到临时表
insert into #temp
select ROW_NUMBER() over(order by id) rownum,* from dbo.Test
where dealerid = '1' and actiontype = 1
 
SELECT * FROM #temp
--遍历临时表数据
declare @i int = 2,@lgh int = (select COUNT(*) from #temp),@actiontime datetime,@actiontime2datetime
 
while @i <= @lgh
begin
      select @actiontime = actiontime from #temp where rownum = @i - 1
      select @actiontime2 = actiontime from #temp where rownum = @i    
      if(ABS(DATEDIFF(s,@actiontime,@actiontime2)) < 60) delete from #temp where rownum =@i                 
      set @i = @i + 1
end
 
select * from #temp

转载于:https://www.cnblogs.com/Amity/p/3153107.html

你可能感兴趣的文章
java语言基础
查看>>
通过weblogic自带脚本正常关闭受管理服务器方法
查看>>
关于最近很火的安卓stagefright漏洞
查看>>
iOS开发那些事-iOS网络编程同步GET方法请求编程
查看>>
pycharm,vim,items2常用快捷键
查看>>
Flask blueprint蓝图按功能模块化架构实例
查看>>
数据支撑环境的改造
查看>>
ifconfig 命令用来查看和配置网络设备
查看>>
用 netstat 查看 TCP 网络状态详解
查看>>
JVM 调优总结:一些概念
查看>>
HTML5:使用Lawnchair库存储JSON
查看>>
如何设计秒杀系统
查看>>
屏蔽USB设备的几种方法
查看>>
zen cart 特价商品列表页中加入排序选择
查看>>
配置Pycharm4.5.4调试edX Devstack
查看>>
Nginx+Tomcat负载均衡配置
查看>>
symbol AP5131重置密码和恢复出厂设置
查看>>
自定义一个jdbc框架
查看>>
[SHELL]shell scripts笔记(2)
查看>>
redis 客户端工具
查看>>