每小时postgres每日数据组

时间:2016-10-22 05:45:21

标签: sql postgresql

嗨,我有这样的表

services
    id | service_name
    1  | service1
    2  | service2
    3  | service3

并且服务有许多令牌

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

并且令牌与token_queue具有一对一的关系

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

现在我想显示令牌表的 generated_time 每小时24小时所有服务的最短/最长时间。这里的最小值/最大值由

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

例如

    hour -  2016-10-21 09:00:00
       service1
         min - 00:01:06
         max - 00:05:00
       service2
          min - 00:01:40
          max - 00:03:00
      service3
          min - 00:01:02
          max - 00:03:12

    hour -  2016-10-21 10:00:00
       service1
         min - 00:01:20
         max - 00:02:50
       service2
          min - 00:01:30
          max - 00:03:45
      service3
          min - 00:02:02
          max - 00:05:12

我试过这个

SELECT date_trunc('hour', t.generated_time) AS hour,
       t.service_id AS service,
       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 service,hour;

但是我可以这样打印

hour - 2016-10-21 09:00:00
  service1
     min - 00:01:06
     max - 00:05:00
hour - 2016-10-21 09:00:00
  service2
     min - 00:01:40
     max - 00:03:00
hour - 2016-10-21 10:00:00
  service1
     min - 00:01:06
     max - 00:05:00
hour - 2016-10-21 10:00:00
  service2
     min - 00:01:30
     max - 00:03:45

感谢您的任何帮助和建议。

2 个答案:

答案 0 :(得分:0)

我通过对数组结果进行分组来管理它

foreach($min_max_avg as $mma){
    $hour = $mma->hour;
        if (isset($result[$hour])) {
                $result[$hour][] = $mma;
        } else {
                 $result[$hour] = array($mma);
                }
}

答案 1 :(得分:0)

你几乎就在那里,不需要重新安排你的结果。

我建议添加service_name以便于打印,并先按小时排序查询结果,然后按service_name(sqlfiddle

排序
select date_trunc('hour', t.generated_time) as hour,
       s.service_name as service,
       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
join services s on s.id = t.service_id
group by service, hour
order by hour, service

现在您可以随时打印结果,只需要记住当前小时并在更改时开始新的部分

$current_hour = 0;
foreach ($min_max_avg as $mma) {
    if ($mma->hour != $current_hour) {
        echo "hour - $mma->hour\n";
        $current_hour = $mma->hour;
    }

    echo "    $mma->service\n";
    echo "        min - $mma->min\n";
    echo "        max - $mma->max\n";
}