You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Use the following SQL-Statements to create the new table:
Table structure for table gymhistory
CREATETABLEIF NOT EXISTS `gymhistory` (
`id`int(11) NOT NULL AUTO_INCREMENT,
`gym_id`varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL,
`team_id`smallint(6) NOT NULL,
`guard_pokemon_id`smallint(6) NOT NULL,
`total_cp`int(11) NOT NULL DEFAULT '0',
`last_modified` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`last_updated` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`pokemon_uids`varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`pokemon_count`smallint(6) NOT NULL,
PRIMARY KEY (`id`),
KEY `gym_id` (`gym_id`),
KEY `team_id` (`team_id`),
KEY `total_cp` (`total_cp`),
KEY `last_modified` (`last_modified`),
KEY `last_updated` (`last_updated`),
KEY `combined` (`gym_id`, `team_id`, `total_cp`, `last_updated`, `pokemon_count`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
Add inital dataset for table gymhistory
INSERT INTO`gymhistory` (
SELECTNULL, g.gym_id, g.team_id, g.guard_pokemon_id, g.total_cp, g.last_modified, g.last_modifiedas last_updated,
(SELECT GROUP_CONCAT(DISTINCT pokemon_uid ORDER BY deployment_time SEPARATOR ',') FROM gymmember AS gm WHEREgm.gym_id=g.gym_idGROUP BY gym_id) AS pokemon_uids,
(SELECTCOUNT(DISTINCT pokemon_uid) FROM gymmember AS gm WHEREgm.gym_id=g.gym_id) AS pokemon_count
FROM gym AS g
);
Use the following SQL-Statements to create the event to update the new table:
Create event gymhistory_update
DELIMITER //
CREATE EVENT IF NOT EXISTS `gymhistory_update`ON SCHEDULE EVERY 15 SECOND
DO BEGININSERT INTO gymhistory (SELECTNULL, g.gym_id, g.team_id, g.guard_pokemon_id, g.total_cp, g.last_modified, g.last_modifiedas last_updated, (SELECT GROUP_CONCAT(DISTINCT pokemon_uid ORDER BY deployment_time SEPARATOR ',') FROM gymmember AS gm WHEREgm.gym_id=g.gym_idGROUP BY gym_id) AS pokemon_uids, (SELECTCOUNT(DISTINCT pokemon_uid) FROM gymmember AS gm WHEREgm.gym_id=g.gym_id) AS pokemon_count FROM gym AS g WHEREg.last_modified> (SELECTMAX(last_modified) FROM gymhistory));
UPDATE gymhistory AS gh
JOIN (SELECT gym_id, MAX(last_modified) as max_last_modified FROM gymhistory GROUP BY gym_id)
AS gg ONgh.gym_id=gg.gym_idANDgh.last_modified=gg.max_last_modifiedJOIN (SELECT gym_id, last_scanned, GROUP_CONCAT(DISTINCT pokemon_uid ORDER BY deployment_time SEPARATOR ',') AS pokemon_uids, COUNT(DISTINCT pokemon_uid) as pokemon_count FROM gymmember AS gm GROUP BY gym_id, last_scanned)
AS gm ONgh.gym_id=gm.gym_idSETgh.last_updated=gm.last_scanned, gh.pokemon_uids=gm.pokemon_uids, gh.pokemon_count=gm.pokemon_countWHEREgh.last_updated<gm.last_scanned;
END
//
DELIMITER ;