2016-10-21 76 views
0

您好我有一個表名令牌這將有這樣Postgres的最小/最大

id  | generated_time   | token_name 
461087 | 2016-10-21 09:02:53.951 | G1 
461088 | 2016-10-21 09:31:13.469 | C1 
461089 | 2016-10-21 09:31:15.711 | C2 
461090 | 2016-10-21 10:37:17.73 | C3 
461091 | 2016-10-21 10:02:53.951 | G2 
461092 | 2016-10-21 10:15:13.469 | C4 
461093 | 2016-10-21 11:22:15.611 | C5 
461094 | 2016-10-21 11:31:14.743 | C6 

和表token_queue像這樣具有以一對一的關係

serving_end_time  | serving_start_time  | token_id 
2016-10-21 09:04:45.681 | 2016-10-21 09:03:49.05 | 461087 
2016-10-21 09:33:49.035 | 2016-10-21 09:32:07.996 | 461088 
2016-10-21 09:34:42.431 | 2016-10-21 09:32:27.134 | 461089 
2016-10-21 10:39:57.775 | 2016-10-21 10:38:37.428 | 461090 
2016-10-21 10:04:49.715 | 2016-10-21 10:03:09.972 | 461091 
2016-10-21 10:17:28.268 | 2016-10-21 10:16:06.946 | 461092 
2016-10-21 11:23:36.036 | 2016-10-21 11:22:30.233 | 461093 
2016-10-21 11:32:32.876 | 2016-10-21 11:31:27.044 | 461094 

生成的令牌的數據現在我想從令牌表中以小時爲單位顯示全天的最小/最大值-24小時(current_date),並根據token_queue計算最小/最大值,即

MIN(token_queue.serving_end_time - token_queue.serving_start_time)
MAX(token_queue.serving_end_time - token_queue.serving_start_time)

這裏是日期2016年10月21日

hour    | min   | max 
    2016-10-21 09:00:00 | 00:01:06 | 00:05:00 
    2016-10-21 10:00:00 | 00:01:16 | 00:04:00 
    2016-10-21 11:00:00 | 00:02:00 | 00:05:26 

感謝您的任何幫助/建議採樣輸出。

回答

0
SELECT date_trunc('hour', t.generated_time) AS hour, 
     min(tq.serving_end_time - tq.serving_start_time) AS min, 
     max(tq.serving_end_time - tq.serving_start_time) AS max 
FROM token t 
    JOIN token_queue tq 
     ON t.id = tq.token_id 
GROUP BY 1 
ORDER BY 1; 
+0

準確,能否請你幫我一個忙多我怎麼可以刪除第二,從時間戳 – sanu

+0

的分數與'date_trunc'包裹。 –