CREATE VIEW PROJ_State_Cases_3_6_7
	(PROJECTION_ID, PROJECTION_NAME, PROJECTION_TYPE, SPHEROID_CODE,
	 PROJECTION_UOM, ZONE_CODE, Start_Date, Stop_Date)
AS
/* cases 3,6, and 7 */
SELECT P2.PROJECTION_ID, P2.PROJECTION_NAME, P2.PROJECTION_TYPE, P2.SPHEROID_CODE,
       P2.PROJECTION_UOM, P2.ZONE_CODE, P1.When_Changed, P2.When_Changed
FROM P_Log AS P1, P_Log AS P2
WHERE P1.PROJECTION_ID = P2.PROJECTION_ID
AND
/* multiple entries in P_Log */
	P1.When_Changed < P2.When_Changed
AND
/* P1 immediatly precedes P2 */
	NOT EXISTS ( 	SELECT *
			FROM P_Log AS P3
			WHERE P3.PROJECTION_ID = P1.PROJECTION_ID
			AND P1.When_Changed < P3.When_Changed
			AND P3.When_Changed < P2.When_Changed )
AND
/* case 3, not in projections */
	(( NOT EXISTS (	SELECT *
			FROM PROJECTIONS
			WHERE PROJECTIONS.PROJECTION_ID = P2.PROJECTION_ID)
		AND
/* P2 is last entry in P_Log */
	   NOT EXISTS (	SELECT *
			FROM P_Log AS P3
			WHERE P3.PROJECTION_ID = P2.PROJECTION_ID
			AND P2.When_Changed < P3.When_Changed ))
OR
/* case 6, P2 is not the last entry */
	EXISTS (SELECT *
		FROM P_Log  AS P3
		WHERE P3.PROJECTION_ID = P2.PROJECTION_ID
		AND P2.When_Changed < P3.When_Changed )
OR
/* case 7, P2 is the last entry */
	( NOT EXISTS (	SELECT *
			FROM P_Log AS P3
			WHERE P3.PROJECTION_ID = P2.PROJECTION_ID
			AND P2.When_Changed < P3.When_Changed )
		AND
/* ID also in PROJECTIONS */
		EXISTS (SELECT *
			FROM PROJECTIONS
			WHERE PROJECTIONS.PROJECTION_ID = P2.PROJECTION_ID )))
