SQL查询连续数字并且统计连续个数和分钟

悠然悠哉 2014-11-06 04:50:36

各位大神,业务大概是这样,统计出来一共停靠多少次,h_id连续为一次,并且算出本次停靠的时间多少分钟?(例如:h_id:4到5是一次停靠,停靠时间为10秒,34到43为一次停靠,停靠时间为:1分30秒),请问这种用SQL可以实现吗?求帮助。。。在线等。。。。
...全文
1114 12 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
axie1999 2017-04-10
  • 打赏
  • 举报
回复
with tmp as ( SELECT 1 id,'1' time FROM dual union all SELECT 2 id,'2' time FROM dual union all SELECT 3 id,'3' time FROM dual union all SELECT 4 id,'4' time FROM dual union all SELECT 7 id,'5' time FROM dual union all SELECT 9 id,'6' time FROM dual union all SELECT 10 id,'7' time FROM dual union all SELECT 11 id,'8' time FROM dual) SELECT id,time FROM tmp a WHERE not exists(SELECT 1 from tmp b WHERE a.id = b.id -1)
kkoofire 2016-01-26
  • 打赏
  • 举报
回复
select c.group_id '断点',datediff(ss,min(c.time),max(c.time)) '停靠次数' from (select x.id,x.time,min(y.id) group_id from @tmp x left join (select id,time from @tmp a where not exists(select 1 from @tmp b where b.id=a.id+1)) y on x.id<=y.id group by x.id,x.time) c group by c.group_id 其中@tmp是原表的表名
悠然悠哉 2014-11-07
  • 打赏
  • 举报
回复
引用 9 楼 sibiyellow 的回复:
下班了 没时间啦 暂写道这

create table a(id int,time datetime)
insert into a 
select 1,'2014-10-01 05:00:06' union all
select 2,'2014-10-01 05:00:26' union all
select 4,'2014-10-01 05:10:06' union all
select 6,'2014-10-01 05:11:06' union all
select 7,'2014-10-01 05:11:20' union all
select 8,'2014-10-01 05:11:30' union all
select 10,'2014-10-01 05:14:06'

Create proc d_gettime
as
begin
	create table #tb(beginid int,endid int,duration int)
	declare @begin int
	declare @begintime datetime
	declare @changebegin int
	declare @end int
	declare @endtime datetime
	set @begin=0
	set @changebegin=0
	declare @item int
	declare @itemvalue datetime
	declare @i int
	set @i=0
	declare mycursor cursor for select id,time from a order by id
	open mycursor
	fetch next from mycursor into @item,@itemvalue
	while (@@fetch_status=0)
	begin
		if(@i=0)
		begin
			set @begin=@item
			set @begintime=@itemvalue
		end
		if(@item<>@changebegin+1 and @changebegin<>0)
		begin
			if(@end-@begin>0)
			begin
				insert into #tb select @begin,@end,DATEDIFF(SS,@begintime,@endtime)
			end
			set @begin=@item
			set @begintime=@itemvalue
		end
		else
		begin
			set @end=@item
			set @endtime=@itemvalue
		end
		set @changebegin=@item
		set @i=@i+1
		fetch next from mycursor into @item,@itemvalue
	end
	close mycursor
	deallocate mycursor	
	select * from #tb
end
--执行存储过程
exec d_gettime
--结果
beginid     endid       duration
----------- ----------- -----------
1           2           20
6           8           24
嗨咯,在吗?
天下如山 2014-11-06
  • 打赏
  • 举报
回复
下班了 没时间啦 暂写道这

create table a(id int,time datetime)
insert into a 
select 1,'2014-10-01 05:00:06' union all
select 2,'2014-10-01 05:00:26' union all
select 4,'2014-10-01 05:10:06' union all
select 6,'2014-10-01 05:11:06' union all
select 7,'2014-10-01 05:11:20' union all
select 8,'2014-10-01 05:11:30' union all
select 10,'2014-10-01 05:14:06'

Create proc d_gettime
as
begin
	create table #tb(beginid int,endid int,duration int)
	declare @begin int
	declare @begintime datetime
	declare @changebegin int
	declare @end int
	declare @endtime datetime
	set @begin=0
	set @changebegin=0
	declare @item int
	declare @itemvalue datetime
	declare @i int
	set @i=0
	declare mycursor cursor for select id,time from a order by id
	open mycursor
	fetch next from mycursor into @item,@itemvalue
	while (@@fetch_status=0)
	begin
		if(@i=0)
		begin
			set @begin=@item
			set @begintime=@itemvalue
		end
		if(@item<>@changebegin+1 and @changebegin<>0)
		begin
			if(@end-@begin>0)
			begin
				insert into #tb select @begin,@end,DATEDIFF(SS,@begintime,@endtime)
			end
			set @begin=@item
			set @begintime=@itemvalue
		end
		else
		begin
			set @end=@item
			set @endtime=@itemvalue
		end
		set @changebegin=@item
		set @i=@i+1
		fetch next from mycursor into @item,@itemvalue
	end
	close mycursor
	deallocate mycursor	
	select * from #tb
end
--执行存储过程
exec d_gettime
--结果
beginid     endid       duration
----------- ----------- -----------
1           2           20
6           8           24
於黾 2014-11-06
  • 打赏
  • 举报
回复
引用 6 楼 yuyongjun890421 的回复:
[quote=引用 3 楼 Z65443344 的回复:] 我觉得从你的数据源头设计的就有问题 为什么是停靠过程中不断的往里写数据 而不是停靠的时候写一条,离开的时候更新另一个字段 这样两个时间一减就直接出结果了
因为我这个在地图上面还是显示轨迹的。。。。[/quote] 这跟你显示不显示轨迹无关 显示轨迹也没必要不断的往里写入啊,已经停了还写什么
悠然悠哉 2014-11-06
  • 打赏
  • 举报
回复
引用 1 楼 sp1234 的回复:
SQL 问题,最好去相关数据库论坛去问。 如果使用c#编程,肯定(对大多数人来说)比SQL方便。如果你不是非要用SQL语句去实现,那么这个东西就会变得很简单的,就是简单地一两层循环而已。
您好,能赐教一下吗?怎么写呢?
悠然悠哉 2014-11-06
  • 打赏
  • 举报
回复
引用 3 楼 Z65443344 的回复:
我觉得从你的数据源头设计的就有问题 为什么是停靠过程中不断的往里写数据 而不是停靠的时候写一条,离开的时候更新另一个字段 这样两个时间一减就直接出结果了
因为我这个在地图上面还是显示轨迹的。。。。
於黾 2014-11-06
  • 打赏
  • 举报
回复
而且你这样统计是很不靠谱的做法 如果巡逻01在一次停靠之后,没有其他车辆再次停靠 然后01又停靠,这个ID就会是连续的 你单纯从ID就没法区分了,还要再判断时间间隔
悠然悠哉 2014-11-06
  • 打赏
  • 举报
回复
引用 2 楼 sibiyellow 的回复:
存储过程就是咯。
能指教一下吗?怎么写呢?谢谢了。。。。
於黾 2014-11-06
  • 打赏
  • 举报
回复
我觉得从你的数据源头设计的就有问题 为什么是停靠过程中不断的往里写数据 而不是停靠的时候写一条,离开的时候更新另一个字段 这样两个时间一减就直接出结果了
天下如山 2014-11-06
  • 打赏
  • 举报
回复
存储过程就是咯。
  • 打赏
  • 举报
回复
SQL 问题,最好去相关数据库论坛去问。 如果使用c#编程,肯定(对大多数人来说)比SQL方便。如果你不是非要用SQL语句去实现,那么这个东西就会变得很简单的,就是简单地一两层循环而已。

62,240

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧