I have a table called 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)
;
I also have another table with measurements
data:
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)
;
As you can see, the table measurements
contains a foreign key historic_rowid
to the historic
table (on the rowid
).
I need to choose a category, say sci-fi
. I want to then, from measurements
select all records matching the sci-fi
category and include their time:
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';
The result would be a table with the following columns:
film_name, category, measurement, time_utc
Now, I want to average this data every 15 minutes. In other words, I want to "bin" my data into 15 minute intervals and then for each "bin", get the average.
My final result will look something like this:
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
....
How can I do this? I'm rather rubbish with SQL and could use some help.
UPDATE
As requested, here is some sample data for the 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