clear markdown compare help best fiddles feedback dbanow.uk a leap of faith?
clear markdown donate comments/suggestions/bugs a leap of faith? bible365 diddy dollings
By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0. 984296 fiddles created (11674 in the last week).

CREATE TABLE main_segments_history ( id_segment integer NOT NULL, day date NOT NULL, day_slices bigint[], CONSTRAINT main_segments_history_pk PRIMARY KEY (id_segment, day) ) ; CREATE TABLE dayly_segments ( id_segment integer NOT NULL, day date NOT NULL, id_slice integer NOT NULL, slice bigint, PRIMARY KEY (id_segment, day, id_slice) ) ;
 hidden batch(es)


INSERT INTO dayly_segments (id_segment, day, id_slice, slice) SELECT id_segment, '2017-01-01', id_slice, (random()*1e7)::bigint FROM generate_series (1, 200) AS s1(id_segment) CROSS JOIN generate_series (1, 20*24) AS s2(id_slice) ;
96000 rows affected
 hidden batch(es)


-- Move segments from dayly_segments to main_segment_history INSERT INTO main_segments_history (id_segment, day, day_slices) SELECT id_segment, day, (SELECT array_agg(slice) FROM (SELECT slice FROM dayly_segments s1 WHERE s1.id_segment = s0.id_segment AND s1.day = s0.day ORDER BY id_slice) AS s2) FROM (SELECT DISTINCT id_segment, day FROM dayly_segments s0 WHERE day = '2017-01-01' ) AS s0 ; -- Delete them from original DELETE FROM dayly_segments WHERE day = '2017-01-01' ;
200 rows affected
96000 rows affected
 hidden batch(es)