2015-06-03 63 views
0

現在我試圖解決這似乎爲我的理解某種程度上與Finding gaps in huge event streams?總結圖表,但略有不同的時間戳

我有我的表中數據的幾個流的問題。我想總結一下時間,但他們並不總是相同的時間戳。該表是這樣的:

模式:

CREATE TABLE Table1 
    ("id" int, "stream_id" int, "timestamp" timestamp, "value" real) 
; 

INSERT INTO Table1 
    ("id", "stream_id", "timestamp", "value") 
VALUES 
    (1, 7, '2015-06-01 15:20:30', 0.1), 
    (2, 7, '2015-06-01 15:20:31', 0.2), 
    (3, 7, '2015-06-01 15:20:32', 0.3), 
    (4, 7, '2015-06-01 15:25:30', 0.5), 
    (5, 7, '2015-06-01 15:25:31', 1.0), 

    (6, 6, '2015-06-01 15:20:31', 1.1), 
    (7, 6, '2015-06-01 15:20:32', 1.2), 
    (8, 6, '2015-06-01 15:20:33', 1.3), 
    (9, 6, '2015-06-01 15:25:31', 1.5), 
    (10, 6, '2015-06-01 15:25:32', 2.0)  
; 

我試圖解決這個問題:

with ts as (select "timestamp"  
      from Table1 
      order by "timestamp" 
     ), 
data as (select "timestamp","value" 
     from Table1 
     order by "timestamp" 
     ), 
streams as (select "stream_id"   
      from Table1 
      group by "stream_id" 
      order by "stream_id" 
     )   
select * .... (question) 

我希望得到一個圖線爲大家總結了數據。當一次在其他流中沒有數據時,總和應該佔用當前時間戳的timestamp < current_timestamp但最近的行。如果沒有價值,假設0

我想過遞歸查詢,但我有點不明白的解決方案......

編輯:在這裏,我試圖以圖形解釋:

graphical description of my problem

編輯2:

我想這樣的事情,但我沒有得到最後的「啄」來完成它。

with RECURSIVE data as (
    select * from rawdata 
    where date(date_time)='2014-05-01' 
), 
streams as (
    select stream_id from data 
    group by stream_id 
), 
t(n) AS (
    VALUES (1) 
    UNION ALL 
    SELECT n+1 FROM t WHERE n < (select count(*) from streams) 
) 
SELECT n FROM t; 
+0

我對你在問什麼有點困惑。你只需要每個流的總和或每個流的總和按時間分配。如果你需要它,你的時間分辨率是多少? I.E.你需要一分鐘的流量總和嗎?5分鐘?等... –

+0

我更新了我的問題,以更好地解釋它。我不想用桶或時間片來總結它們。我不知道如何用英文解釋它,所以我試圖用圖形來解釋它。 –

+0

你想總結多少個數字? 3個或更多?該圖表明每個總和包含3個項。這是否意味着你想總結最後3個數字,每個數字必須來自不同的流? – krokodilko

回答

1

我道歉,有一個在前面的查詢錯誤
這是一個新的,改正後的查詢:

WITH times AS(
    SELECT DISTINCT "timestamp" As tm 
    FROM table1 
) 
SELECT tm, SUM(val) as s_u_m 
FROM (
    SELECT tm, "stream_id", 
      ( SELECT "value" FROM Table1 t2 
       WHERE t2."timestamp" = max(t1."timestamp") 
       AND t2."stream_id" = t1."stream_id" 
       ORDER BY "id" DESC LIMIT 1 
      ) As val 
    FROM times t 
    JOIN table1 t1 
    ON t.tm >= t1."timestamp" 
    GROUP BY tm, "stream_id" 
    order by tm 
) you_must_have_an_alias_here_in_order_to_avoid_the_syntax_error 
GROUP BY tm 
ORDER BY tm; 
; 

,並與3流源數據演示:http://sqlfiddle.com/#!15/30eb8/5


這是一個模擬您的圖形佈局的源表:

| x | id |    timestamp | stream6 | stream7 | stream8 | 
|----|----|------------------------|---------|---------|---------| 
| 1 | 1 | June, 01 2015 15:20:30 | (null) |  0.1 | (null) | 
| 2 | 2 | June, 01 2015 15:20:31 | (null) |  0.2 | (null) | 
| 3 | 3 | June, 01 2015 15:20:31 |  1.1 | (null) | (null) | 
| 4 | 4 | June, 01 2015 15:20:32 | (null) |  0.3 | (null) | 
| 5 | 5 | June, 01 2015 15:20:32 |  1.2 | (null) | (null) | 
| 6 | 11 | June, 01 2015 15:20:32 | (null) | (null) |  2.3 | 
| 7 | 12 | June, 01 2015 15:20:32 | (null) | (null) |  1.1 | 
| 8 | 10 | June, 01 2015 15:20:33 |  1.3 | (null) | (null) | 
| 9 | 13 | June, 01 2015 15:20:33 | (null) | (null) |  1.7 | 
| 10 | 6 | June, 01 2015 15:25:30 | (null) |  0.5 | (null) | 
| 11 | 7 | June, 01 2015 15:25:31 |  1.5 | (null) | (null) | 
| 12 | 8 | June, 01 2015 15:25:31 | (null) |  1 | (null) | 
| 13 | 9 | June, 01 2015 15:25:32 |  2 | (null) | (null) | 

,其結果是:(V(3)是指:值從記錄,其中x = 3)

|      tm |  s_u_m | 
|------------------------|-----------| 
| June, 01 2015 15:20:30 |  0.1 | 0 + v(1) + 0 
| June, 01 2015 15:20:31 | 1.3000001 | v(3) + v(2) + 0 
| June, 01 2015 15:20:32 |  2.6 | v(5) + v(4) + v(7) => see note below !!! 
| June, 01 2015 15:20:33 |  3.3 | v(8) + v(4) + v(9) 
| June, 01 2015 15:25:30 |  3.5 | v(8) + v(10)+ v(9) 
| June, 01 2015 15:25:31 |  4.2 | v(11)+ v(12)+ v(9) 
| June, 01 2015 15:25:32 |  4.7 | v(13)+ v(12)+ v(9) 

註記記錄| June, 01 2015 15:20:32 | 2.6 |

演示中的源表包含兩個具有相同日期和相同source_id的記錄:

| 6 | 11 | June, 01 2015 15:20:32 | (null) | (null) |  2.3 | 
| 7 | 12 | June, 01 2015 15:20:32 | (null) | (null) |  1.1 | 

查詢拿起唯一的最新記錄X = 7,由於ORDER BY "id" DESC在這個代碼片段:

( SELECT "value" FROM Table1 t2 
     WHERE t2."timestamp" = max(t1."timestamp") 
     AND t2."stream_id" = t1."stream_id" 
     ORDER BY "id" DESC LIMIT 1 
    ) As val 

如果你想拿起的第一條記錄X = 6,而不是最新的,然後從order by子句中刪除DESC

如果要總結具有相同的日期,並把stream_id所有記錄(在上面的例子 - 記錄6 + 7),那麼上面的查詢更改爲:

( SELECT SUM("value") FROM Table1 t2 
     WHERE t2."timestamp" = max(t1."timestamp") 
     AND t2."stream_id" = t1."stream_id" 
    ) As val 

如果你想拿起一個隨機記錄,然後使用ORDER BY random()

+0

由於我提供的樣本數據並未滿足「時間戳」和「時間戳」的要求,所以在小提琴是不正確的。 –

+0

這裏我改變了輸入數據的要求,但它似乎是不正確的。 http://sqlfiddle.com/#!15/00ae2/1/0 –

+0

我已更正查詢,請看看它。 – krokodilko