我怎样才能每15分钟对这些数据进行平均一次?

时间:2017-08-21 09:57:45

标签: postgresql

我有一个名为historic的表格:

create table historic
(
    id serial not null
        constraint table_name_pkey
            primary key,
    film_name varchar,
    category varchar,
    time_utc timestamp
)
;

create unique index table_name_id_uindex
    on historic (id)
;

我还有另一张包含measurements数据的表格:

create table measurements
(
    id serial not null
        constraint measurements_pkey
            primary key,
    historic_rowid integer not null
        constraint measurements_historic_id_fk
            references historic,
    measurement double precision
)
;

create unique index measurements_id_uindex
    on measurements (id)
;

如您所见,表measurements包含historic_rowid表的historic外键rowid

我需要选择一个类别,比如sci-fi。我想从measurements选择与sci-fi类别匹配的所有记录并包含他们的时间:

SELECT h.film_name, h.category, m.measurement, h.time_utc
FROM historic h
LEFT JOIN measurements m on m.historic_rowid == h.id
WHERE h.category = 'sci-fi';

结果将是一个包含以下列的表:

film_name, category, measurement, time_utc

现在,我想每15分钟对这些数据进行平均一次。换句话说,我想" bin"我的数据间隔15分钟,然后每个" bin"得到平均值。

我的最终结果将如下所示:

film_name, category, measurement, time_window
---------------------------------------------
film_a,    sci-fi,    0.234234,    0_to_15
film_b,    sci-fi,    0.692859,    15_to_30
film_c,    sci-fi,    0.875854,    30_to_45
film_d,    sci-fi,    0.583465,    45_to_60
film_e,    sci-fi,    0.265334,    60_to_75
film_f,    sci-fi,    0.152545,    75_to_90
....

我该怎么做?我对SQL很垃圾,可以使用一些帮助。

更新

根据要求,以下是time_utc field的一些示例数据:

2017-04-18 02:31:03
2017-04-18 02:31:12
2017-04-18 02:31:27
2017-04-18 02:31:38
2017-04-18 02:31:53
2017-04-18 02:32:08
2017-04-18 02:32:17
2017-04-18 02:32:22
2017-04-18 02:32:58
2017-04-18 02:33:07
2017-04-18 02:33:12
2017-04-18 02:33:22
2017-04-18 02:33:37
2017-04-18 02:33:47
2017-04-18 02:34:32
2017-04-18 02:34:43
2017-04-18 02:34:47
2017-04-18 02:34:58
2017-04-18 02:35:02
2017-04-18 02:35:12
2017-04-18 02:35:17
2017-04-18 02:35:22
2017-04-18 02:35:32
2017-04-18 02:35:37
2017-04-18 02:35:42
2017-04-18 02:35:52

1 个答案:

答案 0 :(得分:0)

with m15 as (select generate_series('2017-04-18 00:00:00','2017-04-18 00:00:00','15 minutes'::interval) g)
SELECT h.film_name, h.category, avg(m.measurement), g
FROM historic h
LEFT JOIN measurements m on m.historic_rowid == h.id
join m15 on m15.g > time_utc and m15.g + '15 minutes'::interval < time_utc
WHERE h.category = 'sci-fi'
group by h.film_name, h.category, g
如果你想要包含空间隔,

join可能需要是外连接。并且您需要为generate_series定义最小值和最大值 - 可以使用select min(time_utc) and max(time_utc)