/* Place_Details-Convert_to_Place.sql
Converts Place Details + Places into Places; preserves apostrophes
 through some tricky substitutions but other characters may be problematic.
2012-01-22 ve3meo

Three steps:
1. Update EventTable SET PlaceID=SiteID, SiteID=0 WHERE SiteID>0
2. Auto generate a statement for each Place Detail of the form
    Update PlaceTable SET Name = SiteName, PlaceName, PlaceType = 0, MasterID = 0
3. Manually copy the resulting series of Update Statements to a SQLite editor and run them
*/
-- Step 1 Open database and run this script
UPDATE EventTable SET PlaceID=SiteID, SiteID=0 WHERE SiteID>0
;
-- Step 2 automatically generates the UPDATE statements for each Place Detail
SELECT 'UPDATE PlaceTable SET Name = ''' 
       || REPLACE(Site.Name || ', ' || Place.Name, '''', '''''') || ''''
       || ', PlaceType = 0, MasterID = 0 
       WHERE PlaceID = ' || Site.PlaceID || ';' 
       AS Statement
FROM PlaceTable AS Site INNER JOIN PlaceTable AS Place ON Site.MasterID = Place.PlaceID
; 
-- Step 3 Copy the resulting UPDATE statements to your SQLite Editor and run them against the database