-- OrphanedPlaceDetails_DeleteUnused.sql
-- 2012-02-22 ve3meo
-- Deletes orphaned Place Details (having no parent Place)
-- that are NOT used by any event in the EventTable.

DELETE FROM PlaceTable
WHERE PlaceID
IN
(
 SELECT PlaceID
 FROM PlaceTable
 WHERE PlaceType = 2
 AND
 (
  MasterID = 0
  OR
  MasterID NOT IN
 (
  SELECT PlaceID
  FROM PlaceTable
  WHERE PlaceType = 0
 )
)
AND PlaceID 
NOT IN
(Select SiteID 
 FROM EventTable
 )
)
;

SELECT 'Completed deleting Unused Orphaned Place Details' 
AS Status
;