2013-11-27 37 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。 –

相關問題