
/* Code Fragment 6.23 Bob was promoted to Director of Computer Center for 1997 (sequenced version). */

INSERT INTO INCUMBENTS 
SELECT '111-22-3333', PCN, '01/01/98', END_DATE
FROM INCUMBENTS
WHERE SSN = '11-222-3333'
      AND START_DATE <= '01/01/1998'
      AND END_DATE > '01/01/1998'

INSERT INTO INCUMBENTS
SELECT '111-22-3333', PCN, START_DATE, '01/01/1997'
FROM INCUMBENTS
WHERE SSN = '111-22-3333'
      AND START_DATE <= '01/01/1997'
      AND END_DATE > '01/01/1997'

UPDATE INCUMBENTS 
SET PCN = (SELECT PCN
           FROM POSITIONS, JOB_TITLES
           WHERE POSITIONS.JOB_TITLE_CODE1 = JOB_TITLE_CODE
           AND JOB_TITLE = "Director, Computer Center"
           AND POSITIONS.START_DATE <= '01/01/1997'
           AND '01/01/1997' < POSITIONS.END_DATE)
WHERE SSN = '111-22-3333'
      AND START_DATE <= '01/01/1998'
      AND END_DATE > '01/01/1997'

INSERT INTO INCUMBENTS
SELECT '111-22-3333', POSITIONS.PCN, POSITIONS.START_DATE, POSITIONS.END_DATE
FROM INCUMBENTS, POSITIONS, JOB_TITLES
WHERE 	SSN = '111-22-3333'
	AND INCUMBENTS.START_DATE <= '1998-01-01'
	AND INCUMBENTS.END_DATE > '1998-01-01'
	AND POSITIONS.JOB_TITLE_CODE1 = JOB_TITLE_CODE
	AND JOB_TITLE = "Director, Computer Center"
	AND POSITIONS.START_DATE >= '01/01/1997'
	AND INCUMBENTS.START_DATE < POSITIONS.START_DATE
	AND POSITIONS.END_DATE < INCUMBENTS.END_DATE
	AND '01/01/1997' < POSITIONS.END_DATE

INSERT INTO INCUMBENTS
SELECT '111-22-3333', POSITIONS.PCN, POSITIONS.START_DATE, '1998-01-01'
FROM INCUMBENTS, POSITIONS, JOB_TITLES
WHERE 	SSN = '111-22-3333'
	AND INCUMBENTS.START_DATE <= '1998-01-01'
	AND INCUMBENTS.END_DATE > '1998-01-01'
	AND POSITIONS.JOB_TITLE_CODE1 = JOB_TITLE_CODE
	AND JOB_TITLE = "Director, Computer Center"
	AND POSITIONS.START_DATE >= '01/01/1997'
	AND '1998-01-01' < POSITIONS.END_DATE

UPDATE INCUMBENTS
SET START_DATE = '1997-01-01'
WHERE 	SSN = '111-22-3333'
	AND START_DATE < '1997-01-01'
	AND END_DATE > '1997-01-01'

UPDATE INCUMBENTS
SET END_DATE = '1998-01-01'
WHERE 	SSN = '111-22-3333'
	AND START_DATE < '1998-01-01'
	AND END_DATE > '1998-01-01'
	AND NOT EXISTS (SELECT *
			FROM INCUMBENTS AS I2
			WHERE	INCUMBENTS.SSN = I2.SSN
				AND INCUMBENTS.PCN = I2.PCN
				AND INCUMBENTS.START_DATE < I2.END_DATE
				AND I2.START_DATE < INCUMBENTS.END_DATE)
UPDATE INCUMBENTS
SET END_DATE = (SELECT MIN(I2.START_DATE)
		FROM INCUMBENTS AS I2
		WHERE 	INCUMBENTS.SSN = I2.SSN
			AND INCUMBENTS.PCN <> I2.PCN
			AND INCUMBENTS.START_DATE < I2.START_DATE)
WHERE 	SSN = '111-22-3333'
	AND START_DATE < '1998-01-01'
	AND END_DATE > '1998-01-01'
	AND EXISTS (	SELECT *
			FROM INCUMBENTS AS I2
			WHERE	INCUMBENTS.SSN = I2.SSN
				AND INCUMBENTS.PCN <> I2.PCN
				AND INCUMBENTS.START_DATE < I2.END_DATE
				AND I2.START_DATE < INCUMBENTS.END_DATE)
