-- PlacesDeleteUnused.sql
/* 
2013-01-21 Tom Holden ve3meo

Extracted from DeletePhantoms.sql
*/
-- Delete Unused Places and Place Details
-- Places
DELETE
FROM PlaceTable
WHERE PlaceID IN (
		SELECT PlaceID
		FROM PlaceTable
		WHERE PlaceTable.PlaceType = 0
		
		EXCEPT
		
		SELECT PlaceID
		FROM EventTable
		);

-- Place Details
DELETE
FROM PlaceTable
WHERE PlaceID IN (
		SELECT PlaceID
		FROM PlaceTable
		WHERE PlaceTable.PlaceType = 2
		
		EXCEPT
		
		SELECT SiteID
		FROM EventTable
		);

-- Orphaned Place Details having no Master Place
DELETE
FROM PlaceTable
WHERE PlaceType = 2
	AND MasterID IN (
		SELECT MasterID
		FROM PlaceTable
		WHERE PlaceTable.PlaceType = 2
		
		EXCEPT
		
		SELECT PlaceID
		FROM PlaceTable
		);