Note: Had to split this over 2 posts due to length.
I took a quick stab at this; very rough, and nowhere near a complete solution, but you might be able to use some of it as a base to build on.
Tables:
Users (very simple, no password (hash) field, forename/surname, date user created etc):
CREATE TABLE IF NOT EXISTS Users (
UserID INTEGER PRIMARY KEY,
Username VARCHAR(32) UNIQUE NOT NULL)
Certifications (such as 'BLS', 'ALS - AEMT-P'):
CREATE TABLE IF NOT EXISTS Certifications (
CertificationID INTEGER PRIMARY KEY,
ClassID INT NOT NULl,
Code VARCHAR(16) NOT NULL,
Description VARCHAR(32),
UNIQUE(Code, Description))
UserCertifications (keeping track of which certifications each user has and when those certifications are valid):
CREATE TABLE IF NOT EXISTS UserCertifications (
UserCertificationID INTEGER PRIMARY KEY,
UserID INTEGER NOT NULL REFERENCES Users(UserID),
CertificationID INTEGER NOT NULL REFERENCES Certifications(CertificationID),
ValidFrom DATE NOT NULL,
ValidTo DATE NOT NULL,
UNIQUE (UserID, CertificationID, ValidFrom, ValidTo))
Roles (the positions on the ambulance crew, such as 'MVO', 'CPR'):
CREATE TABLE IF NOT EXISTS Roles (
RoleID INTEGER PRIMARY KEY,
Code VARCHAR(16) UNIQUE NOT NULL)
RoleCertifications (to keep track of which certification is required for each role in the crew):
CREATE TABLE IF NOT EXISTS RoleCertifications (
RoleID INTEGER NOT NULL REFERENCES Roles(RoleID),
ClassID INTEGER NOT NULL REFERENCES Certifications(ClassID),
PRIMARY KEY(RoleID, ClassID))
Shifts (start/end date for each crew, e.g. 2014-01-04 19:00 to 2014-01-04 23:00):
CREATE TABLE IF NOT EXISTS Shifts (
ShiftID INTEGER PRIMARY KEY,
StartDate DATE NOT NULL,
EndDate DATE NOT NULL,
UNIQUE (StartDate, EndDate))
ShiftCrews (the users actually assigned to each role within a given shift). For the moment I stuck the 'Permanent' position in this table, but I think there should be a separate entity for tracking which Permanent positions each user has been assigned to, including the start/end dates for that assignment and when it was created.
CREATE TABLE IF NOT EXISTS ShiftCrews (
ShiftCrewID INTEGER PRIMARY KEY,
ShiftID INTEGER NOT NULL REFERENCES Shifts(ShiftID),
RoleID INTEGER NOT NULL REFERENCES Roles(RoleID),
UserID INTEGER NOT NULL REFERENCES Users(UserID),
Permanent BOOLEAN NOT NULL,
UNIQUE (ShiftID, UserID),
UNIQUE (ShiftID, RoleID))
CoverRequests (to store requests from permanent crew members for cover):
CREATE TABLE IF NOT EXISTS CoverRequests (
CoverRequestID INTEGER PRIMARY KEY,
ShiftCrewID INTEGER NOT NULL UNIQUE REFERENCES ShiftCrews(ShiftCrewID),
OriginalUserID INTEGER NOT NULL REFERENCES Users(UserID),
DateRequested DATE NOT NULL,
NewUserID INTEGER REFERENCES Users(UserID),
DateCoverFound DATE)
Now, populating with sample data:
Users:
INSERT INTO Users (Username) VALUES ('svettel');
INSERT INTO Users (Username) VALUES ('falonso');
INSERT INTO Users (Username) VALUES ('mwebber');
INSERT INTO Users (Username) VALUES ('lhamilton');
INSERT INTO Users (Username) VALUES ('kraikkonen');
INSERT INTO Users (Username) VALUES ('nrosberg');
INSERT INTO Users (Username) VALUES ('rgrosjean');
INSERT INTO Users (Username) VALUES ('fmassa');
INSERT INTO Users (Username) VALUES ('jbutton');
INSERT INTO Users (Username) VALUES ('nhulkenberg');
INSERT INTO Users (Username) VALUES ('sperez');
INSERT INTO Users (Username) VALUES ('pdiresta');
INSERT INTO Users (Username) VALUES ('asutil');
INSERT INTO Users (Username) VALUES ('dricciardo');
INSERT INTO Users (Username) VALUES ('jevergne');
INSERT INTO Users (Username) VALUES ('egutierrez');
INSERT INTO Users (Username) VALUES ('vbottas');
INSERT INTO Users (Username) VALUES ('pmaldonado');
INSERT INTO Users (Username) VALUES ('jbianchi');
INSERT INTO Users (Username) VALUES ('cpic');
INSERT INTO Users (Username) VALUES ('hkovalainen');
INSERT INTO Users (Username) VALUES ('gvdgarde');
INSERT INTO Users (Username) VALUES ('mchilton');
Certifications. I defined constants within my Java code for the ALS/BLS etc ClassIDs (ALS = 1, BLS = 2, MVO = 3, CPR = 4). I've added two separate ALS certifications here (one for AEMT-P and one for AEMT-CC, but as you can see they both have the same ClassID, so a User with either one is able to fill the ALS role on a ShiftCrew):
INSERT INTO Certifications (ClassID, Code, Description) VALUES (1, 'ALS', 'AEMT-P');
INSERT INTO Certifications (ClassID, Code, Description) VALUES (1, 'ALS', 'AEMT-CC');
INSERT INTO Certifications (ClassID, Code, Description) VALUES (2, 'BLS', NULL);
INSERT INTO Certifications (ClassID, Code, Description) VALUES (3, 'MVO', NULL);
INSERT INTO Certifications (ClassID, Code, Description) VALUES (4, 'CPR', NULL);
UserCertifications. I've added some Users with multiple Certifications for 2 reasons: 1) I assume this is actually possible in real life and 2) To test the code to stop a user who is already assigned to a ShiftCrew from being shown as a candidate to fill another Role on that ShiftCrew.
INSERT INTO UserCertifications (UserID, CertificationID, ValidFrom, ValidTo)
SELECT UserID, CertificationID, '2013-05-20', '2014-05-20' FROM USERS JOIN CERTIFICATIONS WHERE USERNAME = 'svettel' AND CODE = 'ALS' AND DESCRIPTION = 'AEMT-P';
INSERT INTO UserCertifications (UserID, CertificationID, ValidFrom, ValidTo)
SELECT UserID, CertificationID, '2013-06-10', '2014-06-10' FROM USERS JOIN CERTIFICATIONS WHERE USERNAME = 'falonso' AND CODE = 'MVO' AND DESCRIPTION IS NULL;
INSERT INTO UserCertifications (UserID, CertificationID, ValidFrom, ValidTo)
SELECT UserID, CertificationID, '2013-02-20', '2014-02-20' FROM USERS JOIN CERTIFICATIONS WHERE USERNAME = 'mwebber' AND CODE = 'CPR' AND DESCRIPTION IS NULL;
INSERT INTO UserCertifications (UserID, CertificationID, ValidFrom, ValidTo)
SELECT UserID, CertificationID, '2013-09-20', '2014-09-20' FROM USERS JOIN CERTIFICATIONS WHERE USERNAME = 'lhamilton' AND CODE = 'ALS' AND DESCRIPTION = 'AEMT-CC';
INSERT INTO UserCertifications (UserID, CertificationID, ValidFrom, ValidTo)
SELECT UserID, CertificationID, '2013-11-15', '2014-11-15' FROM USERS JOIN CERTIFICATIONS WHERE USERNAME = 'nrosberg' AND CODE = 'BLS' AND DESCRIPTION IS NULL;
INSERT INTO UserCertifications (UserID, CertificationID, ValidFrom, ValidTo)
SELECT UserID, CertificationID, '2013-10-15', '2014-10-15' FROM USERS JOIN CERTIFICATIONS WHERE USERNAME = 'nrosberg' AND CODE = 'MVO' AND DESCRIPTION IS NULL;
INSERT INTO UserCertifications (UserID, CertificationID, ValidFrom, ValidTo)
SELECT UserID, CertificationID, '2013-12-12', '2014-12-12' FROM USERS JOIN CERTIFICATIONS WHERE USERNAME = 'kraikkonen' AND CODE = 'BLS' AND DESCRIPTION IS NULL;
INSERT INTO UserCertifications (UserID, CertificationID, ValidFrom, ValidTo)
SELECT UserID, CertificationID, '2013-01-20', '2014-01-20' FROM USERS JOIN CERTIFICATIONS WHERE USERNAME = 'rgrosjean' AND CODE = 'ALS' AND DESCRIPTION = 'AEMT-CC';
INSERT INTO UserCertifications (UserID, CertificationID, ValidFrom, ValidTo)
SELECT UserID, CertificationID, '2013-04-01', '2014-04-01' FROM USERS JOIN CERTIFICATIONS WHERE USERNAME = 'fmassa' AND CODE = 'MVO' AND DESCRIPTION IS NULL; " +
INSERT INTO UserCertifications (UserID, CertificationID, ValidFrom, ValidTo)
SELECT UserID, CertificationID, '2013-02-01', '2014-02-01' FROM USERS JOIN CERTIFICATIONS WHERE USERNAME = 'fmassa' AND CODE = 'BLS' AND DESCRIPTION IS NULL;
INSERT INTO UserCertifications (UserID, CertificationID, ValidFrom, ValidTo)
SELECT UserID, CertificationID, '2013-07-20', '2014-07-20' FROM USERS JOIN CERTIFICATIONS WHERE USERNAME = 'jbutton' AND CODE = 'MVO' AND DESCRIPTION IS NULL;
INSERT INTO UserCertifications (UserID, CertificationID, ValidFrom, ValidTo)
SELECT UserID, CertificationID, '2013-06-15', '2014-06-15' FROM USERS JOIN CERTIFICATIONS WHERE USERNAME = 'nhulkenberg' AND CODE = 'CPR' AND DESCRIPTION IS NULL;
INSERT INTO UserCertifications (UserID, CertificationID, ValidFrom, ValidTo)
SELECT UserID, CertificationID, '2013-08-20', '2014-08-20' FROM USERS JOIN CERTIFICATIONS WHERE USERNAME = 'sperez' AND CODE = 'BLS' AND DESCRIPTION IS NULL;
INSERT INTO UserCertifications (UserID, CertificationID, ValidFrom, ValidTo)
SELECT UserID, CertificationID, '2013-10-01', '2014-10-01' FROM USERS JOIN CERTIFICATIONS WHERE USERNAME = 'pdiresta' AND CODE = 'BLS' AND DESCRIPTION IS NULL;
INSERT INTO UserCertifications (UserID, CertificationID, ValidFrom, ValidTo)
SELECT UserID, CertificationID, '2013-06-01', '2014-06-01' FROM USERS JOIN CERTIFICATIONS WHERE USERNAME = 'pdiresta' AND CODE = 'MVO' AND DESCRIPTION IS NULL;
INSERT INTO UserCertifications (UserID, CertificationID, ValidFrom, ValidTo)
SELECT UserID, CertificationID, '2013-01-10', '2014-01-10' FROM USERS JOIN CERTIFICATIONS WHERE USERNAME = 'asutil' AND CODE = 'MVO' AND DESCRIPTION IS NULL;
INSERT INTO UserCertifications (UserID, CertificationID, ValidFrom, ValidTo)
SELECT UserID, CertificationID, '2013-02-28', '2014-02-28' FROM USERS JOIN CERTIFICATIONS WHERE USERNAME = 'dricciardo' AND CODE = 'MVO' AND DESCRIPTION IS NULL;
INSERT INTO UserCertifications (UserID, CertificationID, ValidFrom, ValidTo)
SELECT UserID, CertificationID, '2013-03-01', '2014-03-01' FROM USERS JOIN CERTIFICATIONS WHERE USERNAME = 'jevergne' AND CODE = 'CPR' AND DESCRIPTION IS NULL;
INSERT INTO UserCertifications (UserID, CertificationID, ValidFrom, ValidTo)
SELECT UserID, CertificationID, '2013-03-01', '2014-03-01' FROM USERS JOIN CERTIFICATIONS WHERE USERNAME = 'jevergne' AND CODE = 'BLS' AND DESCRIPTION IS NULL;
INSERT INTO UserCertifications (UserID, CertificationID, ValidFrom, ValidTo)
SELECT UserID, CertificationID, '2013-07-20', '2014-07-20' FROM USERS JOIN CERTIFICATIONS WHERE USERNAME = 'egutierrez' AND CODE = 'MVO' AND DESCRIPTION IS NULL;
INSERT INTO UserCertifications (UserID, CertificationID, ValidFrom, ValidTo)
SELECT UserID, CertificationID, '2013-02-20', '2014-02-20' FROM USERS JOIN CERTIFICATIONS WHERE USERNAME = 'vbottas' AND CODE = 'ALS' AND DESCRIPTION = 'AEMT-P';
INSERT INTO UserCertifications (UserID, CertificationID, ValidFrom, ValidTo)
SELECT UserID, CertificationID, '2013-11-03', '2014-11-03' FROM USERS JOIN CERTIFICATIONS WHERE USERNAME = 'pmaldonado' AND CODE = 'BLS' AND DESCRIPTION IS NULL;
INSERT INTO UserCertifications (UserID, CertificationID, ValidFrom, ValidTo)
SELECT UserID, CertificationID, '2013-06-20', '2014-06-20' FROM USERS JOIN CERTIFICATIONS WHERE USERNAME = 'jbianchi' AND CODE = 'CPR' AND DESCRIPTION IS NULL;
INSERT INTO UserCertifications (UserID, CertificationID, ValidFrom, ValidTo)
SELECT UserID, CertificationID, '2013-04-30', '2014-04-30' FROM USERS JOIN CERTIFICATIONS WHERE USERNAME = 'cpic' AND CODE = 'MVO' AND DESCRIPTION IS NULL;
INSERT INTO UserCertifications (UserID, CertificationID, ValidFrom, ValidTo)
SELECT UserID, CertificationID, '2013-02-20', '2014-02-20' FROM USERS JOIN CERTIFICATIONS WHERE USERNAME = 'hkovalainen' AND CODE = 'MVO' AND DESCRIPTION IS NULL;
INSERT INTO UserCertifications (UserID, CertificationID, ValidFrom, ValidTo) " +
SELECT UserID, CertificationID, '2013-01-10', '2014-01-10' FROM USERS JOIN CERTIFICATIONS WHERE USERNAME = 'gvdgarde' AND CODE = 'CPR' AND DESCRIPTION IS NULL;
INSERT INTO UserCertifications (UserID, CertificationID, ValidFrom, ValidTo)
SELECT UserID, CertificationID, '2013-01-10', '2014-01-10' FROM USERS JOIN CERTIFICATIONS WHERE USERNAME = 'gvdgarde' AND CODE = 'MVO' AND DESCRIPTION IS NULL;
INSERT INTO UserCertifications (UserID, CertificationID, ValidFrom, ValidTo)
SELECT UserID, CertificationID, '2013-04-19', '2014-04-19' FROM USERS JOIN CERTIFICATIONS WHERE USERNAME = 'mchilton' AND CODE = 'MVO' AND DESCRIPTION IS NULL;
Roles:
INSERT INTO Roles (Code) VALUES ('ALS');
INSERT INTO Roles (Code) VALUES ('BLS');
INSERT INTO Roles (Code) VALUES ('MVO');
INSERT INTO Roles (Code) VALUES ('CPR');
INSERT INTO Roles (Code) VALUES ('5TH');
RoleCertifications. Note that the 'BLS' role has both the BLS and ALS ClassIDs and the '5TH' role has the ALS/BLS/MVO and CPR ClassIDs, since it can be filled by anyone. Also, I'm again using the ClassID constants defined above (ALS = 1, BLS = 2, MVO = 3, CPR = 4).
INSERT INTO RoleCertifications (RoleID, ClassID) SELECT ROLEID, 1 FROM ROLES WHERE CODE = 'ALS';
INSERT INTO RoleCertifications (RoleID, ClassID) SELECT ROLEID, 2 FROM ROLES WHERE CODE = 'BLS';
INSERT INTO RoleCertifications (RoleID, ClassID) SELECT ROLEID, 1 FROM ROLES WHERE CODE = 'BLS';
INSERT INTO RoleCertifications (RoleID, ClassID) SELECT ROLEID, 3 FROM ROLES WHERE CODE = 'MVO';
INSERT INTO RoleCertifications (RoleID, ClassID) SELECT ROLEID, 4 FROM ROLES WHERE CODE = 'CPR';
INSERT INTO RoleCertifications (RoleID, ClassID) SELECT ROLEID, 1 FROM ROLES WHERE CODE = '5TH';
INSERT INTO RoleCertifications (RoleID, ClassID) SELECT ROLEID, 2 FROM ROLES WHERE CODE = '5TH';
INSERT INTO RoleCertifications (RoleID, ClassID) SELECT ROLEID, 3 FROM ROLES WHERE CODE = '5TH';
INSERT INTO RoleCertifications (RoleID, ClassID) SELECT ROLEID, 4 FROM ROLES WHERE CODE = '5TH';
Shifts. I wrote a quick bit of Java to populate the Shifts table. I defined a start and end date (the 3rd of February and 9th of February 2014 respectively) and I went through from the 3rd to the 9th adding shifts based on the day of the week. For weekdays I added 2 shifts per day (19:00-23:00 and 23:00-07:00 the next morning). For weekends I added 4 shifts per day (07:00-13:00, 13:00-19:00, 19:00-23:00 and 23:00-07:00 the next morning). This is obviously a naive implementation (it doesn't respect holidays or special shifts), but obviously it was done for simplicity.
INSERT INTO Shifts (StartDate, EndDate) VALUES ('2014-02-03 23:00', '2014-02-04 07:00');
INSERT INTO Shifts (StartDate, EndDate) VALUES ('2014-02-04 19:00', '2014-02-04 23:00');
INSERT INTO Shifts (StartDate, EndDate) VALUES ('2014-02-04 23:00', '2014-02-05 07:00');
INSERT INTO Shifts (StartDate, EndDate) VALUES ('2014-02-05 19:00', '2014-02-05 23:00');
INSERT INTO Shifts (StartDate, EndDate) VALUES ('2014-02-05 23:00', '2014-02-06 07:00');
INSERT INTO Shifts (StartDate, EndDate) VALUES ('2014-02-06 19:00', '2014-02-06 23:00');
INSERT INTO Shifts (StartDate, EndDate) VALUES ('2014-02-06 23:00', '2014-02-07 07:00');
INSERT INTO Shifts (StartDate, EndDate) VALUES ('2014-02-07 19:00', '2014-02-07 23:00');
INSERT INTO Shifts (StartDate, EndDate) VALUES ('2014-02-07 23:00', '2014-02-08 07:00');
INSERT INTO Shifts (StartDate, EndDate) VALUES ('2014-02-08 07:00', '2014-02-08 13:00');
INSERT INTO Shifts (StartDate, EndDate) VALUES ('2014-02-08 13:00', '2014-02-08 19:00');
INSERT INTO Shifts (StartDate, EndDate) VALUES ('2014-02-08 19:00', '2014-02-08 23:00');
INSERT INTO Shifts (StartDate, EndDate) VALUES ('2014-02-08 23:00', '2014-02-09 07:00');
INSERT INTO Shifts (StartDate, EndDate) VALUES ('2014-02-09 07:00', '2014-02-09 13:00');
INSERT INTO Shifts (StartDate, EndDate) VALUES ('2014-02-09 13:00', '2014-02-09 19:00');
INSERT INTO Shifts (StartDate, EndDate) VALUES ('2014-02-09 19:00', '2014-02-09 23:00');
INSERT INTO Shifts (StartDate, EndDate) VALUES ('2014-02-09 23:00', '2014-02-10 07:00');
ShiftCrews. This is actually assigning Users to particular Roles within Shifts. Again, going back to the mention of the Permanent bit above, this isn't a very good solution, but I just wanted to get some ideas down. To populate this I wrote a simple bit of Java code again for the permanent positions. Basically I gave certain users Shifts (such as weekdays 19:00-23:00) and just went from the StartDate to the EndDate (the 3rd Feb - 9th Feb 2014 adding them to the ShiftCrews on the correct days. If you were going to do this in the real environment you would probably be doing a longer period (such as 6-12 months or whatever), so you'd probably want to do until the end of that period or until their certification for the role expired. Those implementation details are a bit beyond the scope of this post, though.
A couple of non-permanent assignments:
INSERT INTO ShiftCrews (ShiftID, RoleID, UserID, Permanent)
SELECT SHIFTID, ROLEID, USERID, 0 FROM SHIFTS JOIN ROLES JOIN USERS WHERE STARTDATE = '2014-02-03 19:00' AND CODE = '5TH' AND USERNAME = 'nrosberg';
INSERT INTO ShiftCrews (ShiftID, RoleID, UserID, Permanent)
SELECT SHIFTID, ROLEID, USERID, 0 FROM SHIFTS JOIN ROLES JOIN USERS WHERE STARTDATE = '2014-02-03 19:00' AND CODE = 'ALS' AND USERNAME = 'lhamilton';
Permanent assignments:
INSERT INTO ShiftCrews (ShiftID, RoleID, UserID, Permanent) SELECT SHIFTID, ROLEID, USERID, 1 FROM SHIFTS JOIN ROLES JOIN USERS WHERE STARTDATE = '2014-02-03 23:00' AND CODE = 'MVO' AND USERNAME = 'egutierrez';
INSERT INTO ShiftCrews (ShiftID, RoleID, UserID, Permanent) SELECT SHIFTID, ROLEID, USERID, 1 FROM SHIFTS JOIN ROLES JOIN USERS WHERE STARTDATE = '2014-02-03 23:00' AND CODE = 'CPR' AND USERNAME = 'jbianchi';
INSERT INTO ShiftCrews (ShiftID, RoleID, UserID, Permanent) SELECT SHIFTID, ROLEID, USERID, 1 FROM SHIFTS JOIN ROLES JOIN USERS WHERE STARTDATE = '2014-02-03 23:00' AND CODE = 'ALS' AND USERNAME = 'vbottas';
INSERT INTO ShiftCrews (ShiftID, RoleID, UserID, Permanent) SELECT SHIFTID, ROLEID, USERID, 1 FROM SHIFTS JOIN ROLES JOIN USERS WHERE STARTDATE = '2014-02-03 23:00' AND CODE = 'BLS' AND USERNAME = 'pdiresta';
INSERT INTO ShiftCrews (ShiftID, RoleID, UserID, Permanent) SELECT SHIFTID, ROLEID, USERID, 1 FROM SHIFTS JOIN ROLES JOIN USERS WHERE STARTDATE = '2014-02-03 23:00' AND CODE = '5TH' AND USERNAME = 'sperez';
INSERT INTO ShiftCrews (ShiftID, RoleID, UserID, Permanent) SELECT SHIFTID, ROLEID, USERID, 1 FROM SHIFTS JOIN ROLES JOIN USERS WHERE STARTDATE = '2014-02-03 19:00' AND CODE = 'CPR' AND USERNAME = 'gvdgarde';
INSERT INTO ShiftCrews (ShiftID, RoleID, UserID, Permanent) SELECT SHIFTID, ROLEID, USERID, 1 FROM SHIFTS JOIN ROLES JOIN USERS WHERE STARTDATE = '2014-02-03 19:00' AND CODE = 'MVO' AND USERNAME = 'mchilton';
INSERT INTO ShiftCrews (ShiftID, RoleID, UserID, Permanent) SELECT SHIFTID, ROLEID, USERID, 1 FROM SHIFTS JOIN ROLES JOIN USERS WHERE STARTDATE = '2014-02-04 23:00' AND CODE = 'MVO' AND USERNAME = 'egutierrez';
INSERT INTO ShiftCrews (ShiftID, RoleID, UserID, Permanent) SELECT SHIFTID, ROLEID, USERID, 1 FROM SHIFTS JOIN ROLES JOIN USERS WHERE STARTDATE = '2014-02-04 23:00' AND CODE = 'CPR' AND USERNAME = 'jbianchi';
INSERT INTO ShiftCrews (ShiftID, RoleID, UserID, Permanent) SELECT SHIFTID, ROLEID, USERID, 1 FROM SHIFTS JOIN ROLES JOIN USERS WHERE STARTDATE = '2014-02-04 23:00' AND CODE = 'ALS' AND USERNAME = 'vbottas';
INSERT INTO ShiftCrews (ShiftID, RoleID, UserID, Permanent) SELECT SHIFTID, ROLEID, USERID, 1 FROM SHIFTS JOIN ROLES JOIN USERS WHERE STARTDATE = '2014-02-04 23:00' AND CODE = 'BLS' AND USERNAME = 'pdiresta';
INSERT INTO ShiftCrews (ShiftID, RoleID, UserID, Permanent) SELECT SHIFTID, ROLEID, USERID, 1 FROM SHIFTS JOIN ROLES JOIN USERS WHERE STARTDATE = '2014-02-04 23:00' AND CODE = '5TH' AND USERNAME = 'sperez';
INSERT INTO ShiftCrews (ShiftID, RoleID, UserID, Permanent) SELECT SHIFTID, ROLEID, USERID, 1 FROM SHIFTS JOIN ROLES JOIN USERS WHERE STARTDATE = '2014-02-04 19:00' AND CODE = 'CPR' AND USERNAME = 'gvdgarde';
INSERT INTO ShiftCrews (ShiftID, RoleID, UserID, Permanent) SELECT SHIFTID, ROLEID, USERID, 1 FROM SHIFTS JOIN ROLES JOIN USERS WHERE STARTDATE = '2014-02-04 19:00' AND CODE = 'MVO' AND USERNAME = 'mchilton';
INSERT INTO ShiftCrews (ShiftID, RoleID, UserID, Permanent) SELECT SHIFTID, ROLEID, USERID, 1 FROM SHIFTS JOIN ROLES JOIN USERS WHERE STARTDATE = '2014-02-05 23:00' AND CODE = 'MVO' AND USERNAME = 'egutierrez';
INSERT INTO ShiftCrews (ShiftID, RoleID, UserID, Permanent) SELECT SHIFTID, ROLEID, USERID, 1 FROM SHIFTS JOIN ROLES JOIN USERS WHERE STARTDATE = '2014-02-05 23:00' AND CODE = 'CPR' AND USERNAME = 'jbianchi';
INSERT INTO ShiftCrews (ShiftID, RoleID, UserID, Permanent) SELECT SHIFTID, ROLEID, USERID, 1 FROM SHIFTS JOIN ROLES JOIN USERS WHERE STARTDATE = '2014-02-05 23:00' AND CODE = 'ALS' AND USERNAME = 'vbottas';
INSERT INTO ShiftCrews (ShiftID, RoleID, UserID, Permanent) SELECT SHIFTID, ROLEID, USERID, 1 FROM SHIFTS JOIN ROLES JOIN USERS WHERE STARTDATE = '2014-02-05 23:00' AND CODE = 'BLS' AND USERNAME = 'pdiresta';
INSERT INTO ShiftCrews (ShiftID, RoleID, UserID, Permanent) SELECT SHIFTID, ROLEID, USERID, 1 FROM SHIFTS JOIN ROLES JOIN USERS WHERE STARTDATE = '2014-02-05 23:00' AND CODE = '5TH' AND USERNAME = 'sperez';
INSERT INTO ShiftCrews (ShiftID, RoleID, UserID, Permanent) SELECT SHIFTID, ROLEID, USERID, 1 FROM SHIFTS JOIN ROLES JOIN USERS WHERE STARTDATE = '2014-02-05 19:00' AND CODE = 'CPR' AND USERNAME = 'gvdgarde';
INSERT INTO ShiftCrews (ShiftID, RoleID, UserID, Permanent) SELECT SHIFTID, ROLEID, USERID, 1 FROM SHIFTS JOIN ROLES JOIN USERS WHERE STARTDATE = '2014-02-05 19:00' AND CODE = 'MVO' AND USERNAME = 'mchilton';
INSERT INTO ShiftCrews (ShiftID, RoleID, UserID, Permanent) SELECT SHIFTID, ROLEID, USERID, 1 FROM SHIFTS JOIN ROLES JOIN USERS WHERE STARTDATE = '2014-02-06 23:00' AND CODE = 'MVO' AND USERNAME = 'egutierrez';
INSERT INTO ShiftCrews (ShiftID, RoleID, UserID, Permanent) SELECT SHIFTID, ROLEID, USERID, 1 FROM SHIFTS JOIN ROLES JOIN USERS WHERE STARTDATE = '2014-02-06 23:00' AND CODE = 'CPR' AND USERNAME = 'jbianchi';
INSERT INTO ShiftCrews (ShiftID, RoleID, UserID, Permanent) SELECT SHIFTID, ROLEID, USERID, 1 FROM SHIFTS JOIN ROLES JOIN USERS WHERE STARTDATE = '2014-02-06 23:00' AND CODE = 'ALS' AND USERNAME = 'vbottas';
INSERT INTO ShiftCrews (ShiftID, RoleID, UserID, Permanent) SELECT SHIFTID, ROLEID, USERID, 1 FROM SHIFTS JOIN ROLES JOIN USERS WHERE STARTDATE = '2014-02-06 23:00' AND CODE = 'BLS' AND USERNAME = 'pdiresta';
INSERT INTO ShiftCrews (ShiftID, RoleID, UserID, Permanent) SELECT SHIFTID, ROLEID, USERID, 1 FROM SHIFTS JOIN ROLES JOIN USERS WHERE STARTDATE = '2014-02-06 23:00' AND CODE = '5TH' AND USERNAME = 'sperez';
INSERT INTO ShiftCrews (ShiftID, RoleID, UserID, Permanent) SELECT SHIFTID, ROLEID, USERID, 1 FROM SHIFTS JOIN ROLES JOIN USERS WHERE STARTDATE = '2014-02-06 19:00' AND CODE = 'CPR' AND USERNAME = 'gvdgarde';
INSERT INTO ShiftCrews (ShiftID, RoleID, UserID, Permanent) SELECT SHIFTID, ROLEID, USERID, 1 FROM SHIFTS JOIN ROLES JOIN USERS WHERE STARTDATE = '2014-02-06 19:00' AND CODE = 'MVO' AND USERNAME = 'mchilton';
INSERT INTO ShiftCrews (ShiftID, RoleID, UserID, Permanent) SELECT SHIFTID, ROLEID, USERID, 1 FROM SHIFTS JOIN ROLES JOIN USERS WHERE STARTDATE = '2014-02-07 23:00' AND CODE = 'MVO' AND USERNAME = 'egutierrez';
INSERT INTO ShiftCrews (ShiftID, RoleID, UserID, Permanent) SELECT SHIFTID, ROLEID, USERID, 1 FROM SHIFTS JOIN ROLES JOIN USERS WHERE STARTDATE = '2014-02-07 23:00' AND CODE = 'CPR' AND USERNAME = 'jbianchi';
INSERT INTO ShiftCrews (ShiftID, RoleID, UserID, Permanent) SELECT SHIFTID, ROLEID, USERID, 1 FROM SHIFTS JOIN ROLES JOIN USERS WHERE STARTDATE = '2014-02-07 23:00' AND CODE = 'ALS' AND USERNAME = 'vbottas';
INSERT INTO ShiftCrews (ShiftID, RoleID, UserID, Permanent) SELECT SHIFTID, ROLEID, USERID, 1 FROM SHIFTS JOIN ROLES JOIN USERS WHERE STARTDATE = '2014-02-07 23:00' AND CODE = 'BLS' AND USERNAME = 'pdiresta';
INSERT INTO ShiftCrews (ShiftID, RoleID, UserID, Permanent) SELECT SHIFTID, ROLEID, USERID, 1 FROM SHIFTS JOIN ROLES JOIN USERS WHERE STARTDATE = '2014-02-07 23:00' AND CODE = '5TH' AND USERNAME = 'sperez';
INSERT INTO ShiftCrews (ShiftID, RoleID, UserID, Permanent) SELECT SHIFTID, ROLEID, USERID, 1 FROM SHIFTS JOIN ROLES JOIN USERS WHERE STARTDATE = '2014-02-07 19:00' AND CODE = 'CPR' AND USERNAME = 'gvdgarde';
INSERT INTO ShiftCrews (ShiftID, RoleID, UserID, Permanent) SELECT SHIFTID, ROLEID, USERID, 1 FROM SHIFTS JOIN ROLES JOIN USERS WHERE STARTDATE = '2014-02-07 19:00' AND CODE = 'MVO' AND USERNAME = 'mchilton';
INSERT INTO ShiftCrews (ShiftID, RoleID, UserID, Permanent) SELECT SHIFTID, ROLEID, USERID, 1 FROM SHIFTS JOIN ROLES JOIN USERS WHERE STARTDATE = '2014-02-08 23:00' AND CODE = 'MVO' AND USERNAME = 'egutierrez';
INSERT INTO ShiftCrews (ShiftID, RoleID, UserID, Permanent) SELECT SHIFTID, ROLEID, USERID, 1 FROM SHIFTS JOIN ROLES JOIN USERS WHERE STARTDATE = '2014-02-08 23:00' AND CODE = 'CPR' AND USERNAME = 'jbianchi';
INSERT INTO ShiftCrews (ShiftID, RoleID, UserID, Permanent) SELECT SHIFTID, ROLEID, USERID, 1 FROM SHIFTS JOIN ROLES JOIN USERS WHERE STARTDATE = '2014-02-08 23:00' AND CODE = 'ALS' AND USERNAME = 'vbottas';
INSERT INTO ShiftCrews (ShiftID, RoleID, UserID, Permanent) SELECT SHIFTID, ROLEID, USERID, 1 FROM SHIFTS JOIN ROLES JOIN USERS WHERE STARTDATE = '2014-02-08 23:00' AND CODE = 'BLS' AND USERNAME = 'pdiresta';
INSERT INTO ShiftCrews (ShiftID, RoleID, UserID, Permanent) SELECT SHIFTID, ROLEID, USERID, 1 FROM SHIFTS JOIN ROLES JOIN USERS WHERE STARTDATE = '2014-02-08 23:00' AND CODE = '5TH' AND USERNAME = 'sperez';
INSERT INTO ShiftCrews (ShiftID, RoleID, UserID, Permanent) SELECT SHIFTID, ROLEID, USERID, 1 FROM SHIFTS JOIN ROLES JOIN USERS WHERE STARTDATE = '2014-02-08 07:00' AND CODE = 'BLS' AND USERNAME = 'pmaldonado';
INSERT INTO ShiftCrews (ShiftID, RoleID, UserID, Permanent) SELECT SHIFTID, ROLEID, USERID, 1 FROM SHIFTS JOIN ROLES JOIN USERS WHERE STARTDATE = '2014-02-08 07:00' AND CODE = 'MVO' AND USERNAME = 'hkovalainen';
INSERT INTO ShiftCrews (ShiftID, RoleID, UserID, Permanent) SELECT SHIFTID, ROLEID, USERID, 1 FROM SHIFTS JOIN ROLES JOIN USERS WHERE STARTDATE = '2014-02-08 07:00' AND CODE = 'CPR' AND USERNAME = 'jevergne';
INSERT INTO ShiftCrews (ShiftID, RoleID, UserID, Permanent) SELECT SHIFTID, ROLEID, USERID, 1 FROM SHIFTS JOIN ROLES JOIN USERS WHERE STARTDATE = '2014-02-09 23:00' AND CODE = 'MVO' AND USERNAME = 'egutierrez';
INSERT INTO ShiftCrews (ShiftID, RoleID, UserID, Permanent) SELECT SHIFTID, ROLEID, USERID, 1 FROM SHIFTS JOIN ROLES JOIN USERS WHERE STARTDATE = '2014-02-09 23:00' AND CODE = 'CPR' AND USERNAME = 'jbianchi';
INSERT INTO ShiftCrews (ShiftID, RoleID, UserID, Permanent) SELECT SHIFTID, ROLEID, USERID, 1 FROM SHIFTS JOIN ROLES JOIN USERS WHERE STARTDATE = '2014-02-09 23:00' AND CODE = 'ALS' AND USERNAME = 'vbottas';
INSERT INTO ShiftCrews (ShiftID, RoleID, UserID, Permanent) SELECT SHIFTID, ROLEID, USERID, 1 FROM SHIFTS JOIN ROLES JOIN USERS WHERE STARTDATE = '2014-02-09 23:00' AND CODE = 'BLS' AND USERNAME = 'pdiresta';
INSERT INTO ShiftCrews (ShiftID, RoleID, UserID, Permanent) SELECT SHIFTID, ROLEID, USERID, 1 FROM SHIFTS JOIN ROLES JOIN USERS WHERE STARTDATE = '2014-02-09 23:00' AND CODE = '5TH' AND USERNAME = 'sperez';
INSERT INTO ShiftCrews (ShiftID, RoleID, UserID, Permanent) SELECT SHIFTID, ROLEID, USERID, 1 FROM SHIFTS JOIN ROLES JOIN USERS WHERE STARTDATE = '2014-02-09 07:00' AND CODE = 'BLS' AND USERNAME = 'pmaldonado';
INSERT INTO ShiftCrews (ShiftID, RoleID, UserID, Permanent) SELECT SHIFTID, ROLEID, USERID, 1 FROM SHIFTS JOIN ROLES JOIN USERS WHERE STARTDATE = '2014-02-09 07:00' AND CODE = 'MVO' AND USERNAME = 'hkovalainen';
INSERT INTO ShiftCrews (ShiftID, RoleID, UserID, Permanent) SELECT SHIFTID, ROLEID, USERID, 1 FROM SHIFTS JOIN ROLES JOIN USERS WHERE STARTDATE = '2014-02-09 07:00' AND CODE = 'CPR' AND USERNAME = 'jevergne';
An unfulfilled CoverRequest. If the CoverRequest is fulfilled you would populate the NewUserID and DateCoverFound fields then modify the appropriate row in the ShiftCrew table to change the Permanent value from TRUE to FALSE (1 to 0) and update the UserID to the NewUserID value.
INSERT INTO CoverRequests (ShiftCrewID, OriginalUserID, DateRequested) SELECT ShiftCrewID, U.UserID, '2014-01-04' FROM SHIFTCREWS SC JOIN SHIFTS S ON SC.SHIFTID = S.SHIFTID JOIN ROLES R ON SC.ROLEID = R.ROLEID JOIN USERS U WHERE STARTDATE = '2014-02-06 19:00' AND R.CODE = 'MVO' AND USERNAME = 'mchilton';