7,713
社区成员
发帖
与我相关
我的任务
分享
use Tempdb
go
--> --> 听雨停了-->生成测试数据
if not object_id(N'Tempdb..#tab') is null
drop table #tab
Go
Create table #tab([id] int,[时间] Datetime,[值] int)
Insert #tab
select 1,'2018-1-1 20:03:04',5 union all
select 2,'2018-1-1 20:03:34',7 union all
select 2,'2018-1-1 20:04:04',17 union all
select 2,'2018-1-1 20:04:34',6 union all
select 2,'2018-1-1 20:05:04',9 union all
select 2,'2018-1-1 20:06:34',1 UNION ALL
select 32,'2018-1-1 21:03:04',11 UNION ALL
select 33,'2018-1-1 21:03:34',12 UNION ALL
select 73,'2018-1-1 22:03:08',7 UNION ALL
select 74,'2018-1-1 22:03:38',8
GO
--测试数据结束
;WITH cte AS (
--找到整个表中最小的时间,从这个时间开始算起
Select MIN(时间) as mintime from #tab
),
cte2 AS (
--每个时间减去最小时间得到的秒数
SELECT *,CAST(时间 as date) as dt,datepart(hour,时间) as hr,datediff(second,mintime,时间) as sec
FROM #tab,cte
),
cte3 AS (
--按日期,小时分组得到每个日期,小时中最小的秒数
SELECT dt,hr,MIN(sec) AS minsec FROM cte2
GROUP BY dt,hr
)
--两表连接得到你要的数据
SELECT a.id,a.时间,a.值
FROM cte2 a
INNER JOIN cte3 b ON a.sec=b.minsec AND a.dt=b.dt AND a.hr=b.hr
大致是这样了[/quote]
---------------
你好,需求发生了一点点变化,就是不用每隔一小时采样了,而是每10分钟就采样一次,怎么改改cte2呢
use Tempdb
go
--> --> 听雨停了-->生成测试数据
if not object_id(N'Tempdb..#tab') is null
drop table #tab
Go
Create table #tab([id] int,[时间] Datetime,[值] int)
Insert #tab
select 1,'2018-1-1 20:03:04',5 union all
select 2,'2018-1-1 20:03:34',7 union all
select 2,'2018-1-1 20:04:04',17 union all
select 2,'2018-1-1 20:04:34',6 union all
select 2,'2018-1-1 20:05:04',9 union all
select 2,'2018-1-1 20:06:34',1 UNION ALL
select 32,'2018-1-1 21:03:04',11 UNION ALL
select 33,'2018-1-1 21:03:34',12 UNION ALL
select 73,'2018-1-1 22:03:08',7 UNION ALL
select 74,'2018-1-1 22:03:38',8
GO
--测试数据结束
;WITH cte AS (
--找到整个表中最小的时间,从这个时间开始算起
Select MIN(时间) as mintime from #tab
),
cte2 AS (
--每个时间减去最小时间得到的秒数
SELECT *,CAST(时间 as date) as dt,datepart(hour,时间) as hr,datediff(second,mintime,时间) as sec
FROM #tab,cte
),
cte3 AS (
--按日期,小时分组得到每个日期,小时中最小的秒数
SELECT dt,hr,MIN(sec) AS minsec FROM cte2
GROUP BY dt,hr
)
--两表连接得到你要的数据
SELECT a.id,a.时间,a.值
FROM cte2 a
INNER JOIN cte3 b ON a.sec=b.minsec AND a.dt=b.dt AND a.hr=b.hr
大致是这样了[/quote]
以上是sql server的,没注意你发的是ACCESS版,ACCESS应该是用不了。但是逻辑大概就是上面那样[/quote]
access每关系,我自己调整一下应该没问题,谢谢,但是还有个问题是如果没有'2018-1-1 21:03:04',11这条记录,而是有一条临近时间的如'2018-1-1 21:03:11',12,能不能取出呢?[/quote]
可以,以上代码有这个功能[/quote]
谢谢,已经修改为access的sql了,就是想要的查询结果,但是access里面没有临时表和存储过程之类的,只能编写成查询,但是用程序调用这个查询的时候,速度上有点差强人意,不知道access里面的查询能不能添加索引呢?
特别是创建下面这个查询的时候,速度有些忙,程序要进入等待状态
cte3 AS (
--按日期,小时分组得到每个日期,小时中最小的秒数
SELECT dt,hr,MIN(sec) AS minsec FROM cte2
GROUP BY dt,hr
大神们有没有什么变法,或者是有没有简单高效一点的单机数据库介绍来用用。
use Tempdb
go
--> --> 听雨停了-->生成测试数据
if not object_id(N'Tempdb..#tab') is null
drop table #tab
Go
Create table #tab([id] int,[时间] Datetime,[值] int)
Insert #tab
select 1,'2018-1-1 20:03:04',5 union all
select 2,'2018-1-1 20:03:34',7 union all
select 2,'2018-1-1 20:04:04',17 union all
select 2,'2018-1-1 20:04:34',6 union all
select 2,'2018-1-1 20:05:04',9 union all
select 2,'2018-1-1 20:06:34',1 UNION ALL
select 32,'2018-1-1 21:03:04',11 UNION ALL
select 33,'2018-1-1 21:03:34',12 UNION ALL
select 73,'2018-1-1 22:03:08',7 UNION ALL
select 74,'2018-1-1 22:03:38',8
GO
--测试数据结束
;WITH cte AS (
--找到整个表中最小的时间,从这个时间开始算起
Select MIN(时间) as mintime from #tab
),
cte2 AS (
--每个时间减去最小时间得到的秒数
SELECT *,CAST(时间 as date) as dt,datepart(hour,时间) as hr,datediff(second,mintime,时间) as sec
FROM #tab,cte
),
cte3 AS (
--按日期,小时分组得到每个日期,小时中最小的秒数
SELECT dt,hr,MIN(sec) AS minsec FROM cte2
GROUP BY dt,hr
)
--两表连接得到你要的数据
SELECT a.id,a.时间,a.值
FROM cte2 a
INNER JOIN cte3 b ON a.sec=b.minsec AND a.dt=b.dt AND a.hr=b.hr
大致是这样了[/quote]
以上是sql server的,没注意你发的是ACCESS版,ACCESS应该是用不了。但是逻辑大概就是上面那样[/quote]
access每关系,我自己调整一下应该没问题,谢谢,但是还有个问题是如果没有'2018-1-1 21:03:04',11这条记录,而是有一条临近时间的如'2018-1-1 21:03:11',12,能不能取出呢?[/quote]
可以,以上代码有这个功能
use Tempdb
go
--> --> 听雨停了-->生成测试数据
if not object_id(N'Tempdb..#tab') is null
drop table #tab
Go
Create table #tab([id] int,[时间] Datetime,[值] int)
Insert #tab
select 1,'2018-1-1 20:03:04',5 union all
select 2,'2018-1-1 20:03:34',7 union all
select 2,'2018-1-1 20:04:04',17 union all
select 2,'2018-1-1 20:04:34',6 union all
select 2,'2018-1-1 20:05:04',9 union all
select 2,'2018-1-1 20:06:34',1 UNION ALL
select 32,'2018-1-1 21:03:04',11 UNION ALL
select 33,'2018-1-1 21:03:34',12 UNION ALL
select 73,'2018-1-1 22:03:08',7 UNION ALL
select 74,'2018-1-1 22:03:38',8
GO
--测试数据结束
;WITH cte AS (
--找到整个表中最小的时间,从这个时间开始算起
Select MIN(时间) as mintime from #tab
),
cte2 AS (
--每个时间减去最小时间得到的秒数
SELECT *,CAST(时间 as date) as dt,datepart(hour,时间) as hr,datediff(second,mintime,时间) as sec
FROM #tab,cte
),
cte3 AS (
--按日期,小时分组得到每个日期,小时中最小的秒数
SELECT dt,hr,MIN(sec) AS minsec FROM cte2
GROUP BY dt,hr
)
--两表连接得到你要的数据
SELECT a.id,a.时间,a.值
FROM cte2 a
INNER JOIN cte3 b ON a.sec=b.minsec AND a.dt=b.dt AND a.hr=b.hr
大致是这样了[/quote]
以上是sql server的,没注意你发的是ACCESS版,ACCESS应该是用不了。但是逻辑大概就是上面那样[/quote]
access每关系,我自己调整一下应该没问题,谢谢,但是还有个问题是如果没有'2018-1-1 21:03:04',11这条记录,而是有一条临近时间的如'2018-1-1 21:03:11',12,能不能取出呢?
use Tempdb
go
--> --> 听雨停了-->生成测试数据
if not object_id(N'Tempdb..#tab') is null
drop table #tab
Go
Create table #tab([id] int,[时间] Datetime,[值] int)
Insert #tab
select 1,'2018-1-1 20:03:04',5 union all
select 2,'2018-1-1 20:03:34',7 union all
select 2,'2018-1-1 20:04:04',17 union all
select 2,'2018-1-1 20:04:34',6 union all
select 2,'2018-1-1 20:05:04',9 union all
select 2,'2018-1-1 20:06:34',1 UNION ALL
select 32,'2018-1-1 21:03:04',11 UNION ALL
select 33,'2018-1-1 21:03:34',12 UNION ALL
select 73,'2018-1-1 22:03:08',7 UNION ALL
select 74,'2018-1-1 22:03:38',8
GO
--测试数据结束
;WITH cte AS (
--找到整个表中最小的时间,从这个时间开始算起
Select MIN(时间) as mintime from #tab
),
cte2 AS (
--每个时间减去最小时间得到的秒数
SELECT *,CAST(时间 as date) as dt,datepart(hour,时间) as hr,datediff(second,mintime,时间) as sec
FROM #tab,cte
),
cte3 AS (
--按日期,小时分组得到每个日期,小时中最小的秒数
SELECT dt,hr,MIN(sec) AS minsec FROM cte2
GROUP BY dt,hr
)
--两表连接得到你要的数据
SELECT a.id,a.时间,a.值
FROM cte2 a
INNER JOIN cte3 b ON a.sec=b.minsec AND a.dt=b.dt AND a.hr=b.hr
大致是这样了[/quote]
以上是sql server的,没注意你发的是ACCESS版,ACCESS应该是用不了。但是逻辑大概就是上面那样
use Tempdb
go
--> --> 听雨停了-->生成测试数据
if not object_id(N'Tempdb..#tab') is null
drop table #tab
Go
Create table #tab([id] int,[时间] Datetime,[值] int)
Insert #tab
select 1,'2018-1-1 20:03:04',5 union all
select 2,'2018-1-1 20:03:34',7 union all
select 2,'2018-1-1 20:04:04',17 union all
select 2,'2018-1-1 20:04:34',6 union all
select 2,'2018-1-1 20:05:04',9 union all
select 2,'2018-1-1 20:06:34',1 UNION ALL
select 32,'2018-1-1 21:03:04',11 UNION ALL
select 33,'2018-1-1 21:03:34',12 UNION ALL
select 73,'2018-1-1 22:03:08',7 UNION ALL
select 74,'2018-1-1 22:03:38',8
GO
--测试数据结束
;WITH cte AS (
--找到整个表中最小的时间,从这个时间开始算起
Select MIN(时间) as mintime from #tab
),
cte2 AS (
--每个时间减去最小时间得到的秒数
SELECT *,CAST(时间 as date) as dt,datepart(hour,时间) as hr,datediff(second,mintime,时间) as sec
FROM #tab,cte
),
cte3 AS (
--按日期,小时分组得到每个日期,小时中最小的秒数
SELECT dt,hr,MIN(sec) AS minsec FROM cte2
GROUP BY dt,hr
)
--两表连接得到你要的数据
SELECT a.id,a.时间,a.值
FROM cte2 a
INNER JOIN cte3 b ON a.sec=b.minsec AND a.dt=b.dt AND a.hr=b.hr