BEGIN;
INSERT INTO contents_downloads_summary
	SELECT file_no, comic_no, mem_no, med_no, sum(points), sum(price), mp, count(*), ins_ts::date as date
	FROM contents_downloads
	WHERE ins_ts::date <= 'YESTERDAY'
	GROUP BY file_no, comic_no, mem_no, med_no, mp, date;
INSERT INTO contents_downloads_log
	SELECT * FROM contents_downloads
	WHERE ins_ts::date <= 'YESTERDAY';
DELETE FROM contents_downloads
	WHERE ins_ts::date <= 'YESTERDAY';
COMMIT;