2013-11-27 38 views
0

编辑获取最新的数据,以周期性的时间戳

我已经意识到我的问题其实有两个部分:

One of the answers第二个问题使用Postgres的SELECT DISTINCT ON,这意味着我不需要一个组的。我已在下面发布我的解决方案。

我有通常查询获取最新值的数据。但是,如果我每分钟查询一次,回溯到某个时间戳,我需要能够重现所收到的结果。

我真的不知道从哪里开始。我对SQL很少有经验。

CREATE TABLE history 
(
    detected timestamp with time zone NOT NULL, 
    stat integer NOT NULL 
) 

我选择这样的:

SELECT 
detected, 
stat 

FROM history 

WHERE 
detected > '2013-11-26 20:19:58+00'::timestamp 

显然,这给了我,因为在给定时间戳每个结果。我希望每个stat从现在到时间戳最近的分钟。我最接近的意思是“小于”。

对不起,我没有做出任何接近答案的任何地方很好的努力。我对SQL不熟悉,我不知道从哪里开始。

编辑

这个问题,How to group time by hour or by 10 minutes,似乎有所帮助:

SELECT timeslot, MAX(detected) 
FROM 
( 
    SELECT to_char(detected, 'YYYY-MM-DD hh24:MI') timeslot, detected 
    FROM 
    (
     SELECT detected 
     FROM history 
     where 
     detected > '2013-11-28 13:09:58+00'::timestamp 
    ) as foo 
) as foo GROUP BY timeslot 

这让我在一分钟的间隔最近的detected时间戳。

如何获取statMAX在按分钟分组的所有detected上运行,但stat不可访问。

第二编辑

我:

timeslot;max 
"2013-11-28 14:04";"2013-11-28 14:04:05+00" 
"2013-11-28 14:17";"2013-11-28 14:17:22+00" 
"2013-11-28 14:16";"2013-11-28 14:16:40+00" 
"2013-11-28 14:13";"2013-11-28 14:13:31+00" 
"2013-11-28 14:10";"2013-11-28 14:10:02+00" 
"2013-11-28 14:09";"2013-11-28 14:09:51+00" 

我想:

detected;stat 
"2013-11-28 14:04:05+00";123 
"2013-11-28 14:17:22+00";125 
"2013-11-28 14:16:40+00";121 
"2013-11-28 14:13:31+00";118 
"2013-11-28 14:10:02+00";119 
"2013-11-28 14:09:51+00";121 

maxdetected是相同的

+0

你有没有试过MAX(检测到)? –

+0

@SamS这将返回一个结果,但我需要自从时间戳以来经过数分钟的结果。 –

+0

我明白你需要什么。达到此目的的一种方法是将数据复制到另一个数据库,并修改查询以使结果大于第二个表上的最大时间戳。我会尽量使一个sqlfiddle –

回答

1

我可以为您提供这个解决方案:

with t (tstamp, stat) as(
    values 
    ( current_timestamp,       'stat1'), 
    ( current_timestamp - interval '50' second, 'stat2'), 
    ( current_timestamp - interval '100' second, 'stat3'), 
    ( current_timestamp - interval '150' second, 'stat4'), 
    ( current_timestamp - interval '200' second, 'stat5'), 
    ( current_timestamp - interval '250' second, 'stat6') 
) 
select stat, tstamp 
from t 
where tstamp in (
    select max(tstamp) 
    from t 
    group by date_trunc('minute', tstamp) 
); 

但它是Oracle ...也许它可以帮助你反正

+2

我冒昧地将其转换为Postgres语法 –

1

好了另一次尝试:)

我用微软的AdventureWorks DB试过了。我采取了一些其他数据类型,但它应该与datetimeoffset或类似的日期时间一起工作。

所以我尝试了一个循环。尽管您的时间戳小于NOW,请为我选择时间戳和时间戳加上间隔大小之间的数据。由于我得到的数据在一个时间间隔,然后我设置时间戳加上间隔得到下一个,直到while循环到达今天。 也许这是一种方式,如果不是为:)对不起

DECLARE @today date 
DECLARE @yourTimestamp date 
DECLARE @intervalVariable date 

SET @intervalVariable = '2005-01-07' -- start at your timestamp 
SET @today = '2010-12-31' 

WHILE @intervalVariable < @today -- your Timestamp on the left side 
BEGIN 

SELECT FullDateAlternateKey FROM dbo.DimDate 
WHERE FullDateAlternateKey BETWEEN @intervalVariable AND DATEADD(dd,3, @intervalVariable) 

SET @intervalVariable = DATEADD(dd,3, @intervalVariable) -- the three is your intervale 
print 'interval' 
END 
print 'Nothing or finished' 
+0

对不起,我没有解释得很清楚。想象一下,我错过了5分钟轮询我的数据库。我希望能够说“给我stat'的最后5分钟,因为如果我每隔1分钟进行一次轮询,它们就会是这样。 –

+0

呃,请不要使用'BETWEEN' [带日期/时间/时间戳类型](http://sqlblog.com/blogs/aaron_bertrand/archive/2011/10/19/what-do-between-and-the -deve-in-common.aspx),_especially_在SQL Server上。 –

1

我的解决方案结合使用to_char剪裁时间戳为最接近的分钟,并用不同分钟选择第一行:

SELECT DISTINCT ON (timeslot) 
to_char(detected, 'YYYY-MM-DD hh24:MI') timeslot, 
detected, 
stat 

FROM history 

ORDER BY timeslot DESC, detected DESC; 

这是由this answer to 'Select first row in each GROUP BY group?'抵达。

+0

您确定,您每分钟以这种方式获得最新的条目,而不是每分钟随机输入一次,因为您在剪辑日期后进行排序 – Armunin

+0

@Armunin ['SELECT DISTINCT ON'](http://www.postgresql.org/docs/current/interactive/sql-select.html#SQL-DISTINCT)使用'ORDER BY'来为每个时隙选择一行。 –

+0

@Armunin我也修正了语法。我在'timeslot'周围丢失了一些parens。 –

相关问题