INSERT INTO geodb_hierarchies(loc_id, level, id_lvl1, id_lvl2, id_lvl3, id_lvl4, id_lvl5, id_lvl6, id_lvl7, id_lvl8, id_lvl9, valid_since, date_type_since, valid_until, date_type_until) SELECT loc_id, level, CASE WHEN level > 0 THEN 0 ELSE NULL END, CASE WHEN level > 1 THEN 0 ELSE NULL END, CASE WHEN level > 2 THEN 0 ELSE NULL END, CASE WHEN level > 3 THEN 0 ELSE NULL END, CASE WHEN level > 4 THEN 0 ELSE NULL END, CASE WHEN level > 5 THEN 0 ELSE NULL END, CASE WHEN LEVEL > 6 THEN 0 ELSE NULL END, CASE WHEN level > 7 THEN 0 ELSE NULL END, CASE WHEN LEVEL > 8 THEN 0 ELSE NULL END, valid_since, date_type_since, valid_until, date_type_until FROM (SELECT loc.loc_id AS loc_id, dat.text_val::integer AS level, dat.valid_since AS valid_since, dat.date_type_since AS date_type_since, dat.valid_until AS valid_until, dat.date_type_until AS date_type_until FROM geodb_locations AS loc JOIN geodb_textdata AS dat USING (loc_id) WHERE dat.text_type = 400200000) AS loc_dat; UPDATE geodb_hierarchies SET id_lvl1 = loc_id WHERE level = 1; UPDATE geodb_hierarchies AS hier SET id_lvl1 = coalesce( (SELECT CAST(text_val AS integer) FROM geodb_textdata WHERE loc_id = hier.loc_id AND text_type = 400100000), 0 ), id_lvl2 = loc_id WHERE level = 2; UPDATE geodb_hierarchies AS hier SET id_lvl2 = coalesce( (SELECT CAST(text_val AS integer) FROM geodb_textdata WHERE loc_id = hier.loc_id AND text_type = 400100000), 0 ), id_lvl3 = loc_id WHERE level = 3; UPDATE geodb_hierarchies AS hier SET id_lvl3 = coalesce( (SELECT CAST(text_val AS integer) FROM geodb_textdata WHERE loc_id = hier.loc_id AND text_type = 400100000), 0 ), id_lvl4 = loc_id WHERE level = 4; UPDATE geodb_hierarchies AS hier SET id_lvl4 = coalesce( (SELECT CAST(text_val AS integer) FROM geodb_textdata WHERE loc_id = hier.loc_id AND text_type = 400100000), 0 ), id_lvl5 = loc_id WHERE level = 5; UPDATE geodb_hierarchies AS hier SET id_lvl5 = coalesce( (SELECT CAST(text_val AS integer) FROM geodb_textdata WHERE loc_id = hier.loc_id AND text_type = 400100000), 0 ), id_lvl6 = loc_id WHERE level = 6; UPDATE geodb_hierarchies AS hier SET id_lvl6 = coalesce( (SELECT CAST(text_val AS integer) FROM geodb_textdata WHERE loc_id = hier.loc_id AND text_type = 400100000), 0 ), id_lvl7 = loc_id WHERE level = 7; UPDATE geodb_hierarchies AS hier SET id_lvl7 = coalesce( (SELECT CAST(text_val AS integer) FROM geodb_textdata WHERE loc_id = hier.loc_id AND text_type = 400100000), 0 ), id_lvl8 = loc_id WHERE level = 8; UPDATE geodb_hierarchies AS hier SET id_lvl8 = coalesce( (SELECT CAST(text_val AS integer) FROM geodb_textdata WHERE loc_id = hier.loc_id AND text_type = 400100000), 0 ), id_lvl9 = loc_id WHERE level = 9; UPDATE geodb_hierarchies AS hier SET id_lvl1 = coalesce((SELECT id_lvl1 FROM geodb_hierarchies WHERE loc_id = hier.id_lvl2), 0) WHERE level = 3; UPDATE geodb_hierarchies AS hier SET id_lvl1 = coalesce((SELECT id_lvl1 FROM geodb_hierarchies WHERE loc_id = hier.id_lvl3), 0), id_lvl2 = coalesce((SELECT id_lvl2 FROM geodb_hierarchies WHERE loc_id = hier.id_lvl3), 0) WHERE level = 4; UPDATE geodb_hierarchies AS hier SET id_lvl1 = coalesce((SELECT id_lvl1 FROM geodb_hierarchies WHERE loc_id = hier.id_lvl4), 0), id_lvl2 = coalesce((SELECT id_lvl2 FROM geodb_hierarchies WHERE loc_id = hier.id_lvl4), 0), id_lvl3 = coalesce((SELECT id_lvl3 FROM geodb_hierarchies WHERE loc_id = hier.id_lvl4), 0) WHERE level = 5; UPDATE geodb_hierarchies AS hier SET id_lvl1 = coalesce((SELECT id_lvl1 FROM geodb_hierarchies WHERE loc_id = hier.id_lvl5), 0), id_lvl2 = coalesce((SELECT id_lvl2 FROM geodb_hierarchies WHERE loc_id = hier.id_lvl5), 0), id_lvl3 = coalesce((SELECT id_lvl3 FROM geodb_hierarchies WHERE loc_id = hier.id_lvl5), 0), id_lvl4 = coalesce((SELECT id_lvl4 FROM geodb_hierarchies WHERE loc_id = hier.id_lvl5), 0) WHERE level = 6; UPDATE geodb_hierarchies AS hier SET id_lvl1 = coalesce((SELECT id_lvl1 FROM geodb_hierarchies WHERE loc_id = hier.id_lvl6), 0), id_lvl2 = coalesce((SELECT id_lvl2 FROM geodb_hierarchies WHERE loc_id = hier.id_lvl6), 0), id_lvl3 = coalesce((SELECT id_lvl3 FROM geodb_hierarchies WHERE loc_id = hier.id_lvl6), 0), id_lvl4 = coalesce((SELECT id_lvl4 FROM geodb_hierarchies WHERE loc_id = hier.id_lvl6), 0), id_lvl5 = coalesce((SELECT id_lvl5 FROM geodb_hierarchies WHERE loc_id = hier.id_lvl6), 0) WHERE level = 7; UPDATE geodb_hierarchies AS hier SET id_lvl1 = coalesce((SELECT id_lvl1 FROM geodb_hierarchies WHERE loc_id = hier.id_lvl7), 0), id_lvl2 = coalesce((SELECT id_lvl2 FROM geodb_hierarchies WHERE loc_id = hier.id_lvl7), 0), id_lvl3 = coalesce((SELECT id_lvl3 FROM geodb_hierarchies WHERE loc_id = hier.id_lvl7), 0), id_lvl4 = coalesce((SELECT id_lvl4 FROM geodb_hierarchies WHERE loc_id = hier.id_lvl7), 0), id_lvl5 = coalesce((SELECT id_lvl5 FROM geodb_hierarchies WHERE loc_id = hier.id_lvl7), 0), id_lvl6 = coalesce((SELECT id_lvl6 FROM geodb_hierarchies WHERE loc_id = hier.id_lvl7), 0) WHERE level = 8; UPDATE geodb_hierarchies AS hier SET id_lvl1 = coalesce((SELECT id_lvl1 FROM geodb_hierarchies WHERE loc_id = hier.id_lvl8), 0), id_lvl2 = coalesce((SELECT id_lvl2 FROM geodb_hierarchies WHERE loc_id = hier.id_lvl8), 0), id_lvl3 = coalesce((SELECT id_lvl3 FROM geodb_hierarchies WHERE loc_id = hier.id_lvl8), 0), id_lvl4 = coalesce((SELECT id_lvl4 FROM geodb_hierarchies WHERE loc_id = hier.id_lvl8), 0), id_lvl5 = coalesce((SELECT id_lvl5 FROM geodb_hierarchies WHERE loc_id = hier.id_lvl8), 0), id_lvl6 = coalesce((SELECT id_lvl6 FROM geodb_hierarchies WHERE loc_id = hier.id_lvl8), 0), id_lvl7 = coalesce((SELECT id_lvl7 FROM geodb_hierarchies WHERE loc_id = hier.id_lvl8), 0) WHERE level = 9;