--- /dev/null
+-- purge-user-activity
+
+BEGIN;
+
+SET STATEMENT_TIMEOUT = 0;
+
+CREATE TEMPORARY TABLE tmp_usr_activity ON COMMIT DROP AS
+ -- Most recent usr_activity row per usr per etype
+ WITH ranked_entry_per_type_and_user AS (
+ SELECT
+ entry.*,
+ RANK() OVER (
+ PARTITION BY entry.etype, entry.usr
+ ORDER BY event_time DESC
+ ) AS position
+ FROM actor.usr_activity entry
+ )
+ SELECT ranked.id, ranked.usr, ranked.etype, ranked.event_time
+ FROM ranked_entry_per_type_and_user ranked
+ WHERE ranked.position = 1
+ -- keep all events for lyndasip and hooplasip2 authz
+ OR ranked.etype IN (1005, 1006);
+
+TRUNCATE actor.usr_activity;
+
+INSERT INTO actor.usr_activity SELECT * FROM tmp_usr_activity;
+
+-- keep only the most recent even going forward with 2
+-- exceptions for lyndasip and hooplasip2.
+UPDATE config.usr_activity_type
+ SET transient = TRUE WHERE id NOT IN (1005, 1006);
+
+COMMIT;
+
+ANALYZE actor.usr_activity;
+
org-unit-addrs-copy [sip-activity-types] 2016-05-03T14:17:59Z Bill Erickson <berickxx@gmail.com> # Give each org unit its own mailing address
drop-cc-cols [sip-activity-types] 2016-05-03T15:26:50Z Bill Erickson <berickxx@gmail.com> # Drop unneeded CC payment columns
connexion-auth-imports [sip-activity-types] 2016-05-11T15:10:49Z Bill Erickson,,, <berick@teapot> # OCLC Connexion Authority Record Imports Data
+purge-user-activity [sip-activity-types] 2016-04-29T17:07:46Z Bill Erickson <berickxx@gmail.com> # Clean up actor.usr_activity