BEGIN;
INSERT INTO official_visits_summary
	SELECT med_no, mem_uim, mp, php_self, count(*), ins_ts::date as ins_date, comic_no FROM official_visits
	WHERE ins_ts::date <= current_timestamp - interval '1 month'
	GROUP BY med_no, mem_uim, mp, php_self, ins_date, comic_no
	ORDER BY ins_date;
DELETE FROM official_visits WHERE ins_ts::date <= current_timestamp - interval '1 month';
COMMIT;

