BEGIN;
INSERT INTO official_charges_summary
	SELECT mem_no, med_no, charge_no, mp, count(*), ins_ts::date as ins_date FROM official_charges
	WHERE ins_ts::date <= 'YESTERDAY'
	GROUP BY mem_no, med_no, charge_no, mp, ins_date
	ORDER BY ins_date;
INSERT INTO official_charges_log
	SELECT * FROM official_charges
	WHERE ins_ts::date <= 'YESTERDAY';
DELETE FROM official_charges WHERE ins_ts::date <= 'YESTERDAY';
COMMIT;