I have a challenge that I've found some solutions that are close, but haven't been able solve it completely (something I've been working on something based on the solution to this question: Check for x consecutive days - given timestamps in database).
Basically, I have teams that run drills. There are multiple members of each team, and a person can be on multiple teams. We have different levels of drills. Some drills are run every day, some are not run every day, and some are run multiple times per day. My task is to count consecutive successful sessions of each drill level for each team member.
This table tracks all sessions:
CREATE TABLE IF NOT EXISTS `readiness_sessions` (
`readiness_sessions_id` int(11) NOT NULL AUTO_INCREMENT,
`session_initiation_time` datetime DEFAULT NULL,
`session_level` varchar(3) DEFAULT NULL,
`session_type` varchar(3) DEFAULT NULL)
Three Sessions:
INSERT INTO readiness_sessions
(session_initiation_time, session_level, session_type)
VALUES
('2015-05-30 15:00:00', '1', '1'),
('2015-06-01 12:15:00', '2', '1'),
('2015-06-01 15:00:00', '1', '1')
For each session created, a record in the following table is created for each team that qualifies for the drill session:
CREATE TABLE IF NOT EXISTS `readiness_team_checkins` (
`readiness_team_checkins_id` int(11) NOT NULL AUTO_INCREMENT,
`readiness_sessions_id` int(11) NOT NULL,
`team_id` int(11) NOT NULL)
Some team checkins (there is additional data in this table, but not relevant to the current challenge):
INSERT INTO readiness_team_checkins
(readiness_sessions_id, team_id)
VALUES
(1,1), (1,2), (1,3),
(2,1), (2,2),
(3,1), (3,2), (3,3)
And for each member of each team, a record is created in this table:
CREATE TABLE IF NOT EXISTS `readiness_team_member_checkins` (
`readiness_team_member_checkins_id` int(11) NOT NULL AUTO_INCREMENT,
`readiness_sessions_id` int(11) NOT NULL,
`readiness_team_checkins_id` int(11) NOT NULL,
`team_member_id` int(11) NOT NULL,
`status` enum('U','D','O','M','F','X') DEFAULT NULL)
Some team member checkin data:
INSERT INTO readiness_team_team_member_checkins
(readiness_sessions_id, readiness_team_checkins_id, team_member_id, status)
VALUES
(1, 1, 1, 'U'), (1, 1, 2, 'M'), (1, 1, 3, 'U'),
(1, 2, 4, 'U'), (1, 2, 5, 'U'), (1, 2, 6, 'U'),
(2, 1, 1, 'U'), (2, 1, 2, 'U'), (2, 1, 3, 'U'),
(2, 2, 4, 'O'), (2, 2, 5, 'M'), (2, 2, 6, 'U'),
(3, 1, 1, 'U'), (3, 1, 2, 'U'), (3, 1, 3, 'U'),
(3, 2, 4, 'M'), (3, 2, 5, 'M'), (3, 2, 6, 'U')
This table is updated with the 'status' of the member. At a base level, I want to find what team members have completed 15, 30, 60, etc consecutive sessions with status 'U' and also find the maximum num for each member.
With the included data sample I'd want to be able to see the following, for example: Team Member 1 would have 2 consecutive successful level 1 drills, and 1 successful level 2 drill. These would be queried for separately.
I'm currently working with existing data. I could modify tables to simplify moving forward, but I'll also need to be able to pull this out of the existing data set.
Is there a way I can run a single query to see if, for example, team_member_id X has 15 consecutive successful drills for session_level Z that is efficient? I haven't been able to come up with anything that works with the multiple joins I believe I'd need.
Any help on this is greatly appreciated!