postgresql - How can I average this data every 15 minutes? -


i have 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 have table 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 can see, table measurements contains foreign key historic_rowid historic table (on rowid).

i need choose category, sci-fi. want then, measurements select records matching sci-fi category , include time:

select h.film_name, h.category, m.measurement, h.time_utc historic h left join measurements m on m.historic_rowid == h.id h.category = 'sci-fi'; 

the result table following columns:

film_name, category, measurement, time_utc 

now, want average data every 15 minutes. in other words, want "bin" data 15 minute intervals , each "bin", average.

my final result 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 this? i'm rather rubbish sql , use help.

update

as requested, here sample data 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 

with m15 (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 historic h left join measurements m on m.historic_rowid == h.id join m15 on m15.g > time_utc , m15.g + '15 minutes'::interval < time_utc h.category = 'sci-fi' group h.film_name, h.category, g 

join need outer join if want include empty intervals of course. , need define minimum , maximum generate_series - doable select min(time_utc) , max(time_utc)


Comments

Popular posts from this blog

python - Operations inside variables -

Generic Map Parameter java -

arrays - What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? -