
/* Code Fragment 6.6 Fill holes in the POSITIONS table for the position of Associate Director of the Computer Center, in the general case. */

INSERT INTO POSITIONS
SELECT '999071', ' ', END_DATE AS STAR_DATE,
	COALESCE ((	SELECT MIN(START_DATE)
			FROM POSITIONS AS P2
			WHERE P2.PCN = '999071'
			AND P2.START_DATE > P.START_DATE), '3000-01-01') AS END_DATE
FROM POSITIONS AS P
WHERE P.PCN = '999071'
AND P.END_DATE > GETDATE()
AND P.END_DATE < '3000-01-01'
AND NOT EXISTS (SELECT *
		FROM POSITIONS AS P3
		WHERE P3.PCN = '999071'
		AND P3.START_DATE <= P.END_DATE
		AND P.END_DATE < P3.END_DATE)
