INSERT INTO "geonames" ("admin1", "admin2", "admin3", "asciiname", "country_code", "created_at", "dem", "elevation", "feature", "geoname_id", "latitude", "longitude", "modified_on", "name", "population", "timezone", "updated_at") VALUES ('admin1_07', 'admin2_05', 'admin3_03', 'Geoname_ascii21', 'CM', '2014-12-09 09:46:31.158343', 556, 170, 'AADM3', 21, -87.5215329463218, -45.1375812771716, '2014-12-03', 'Geoname021', 39451, 'Time/Zone21', '2014-12-09 09:46:31.158343') RETURNING "geoname_id" INSERT INTO "geonames" ("admin1", "admin2", "admin3", "admin4", "asciiname", "country_code", "created_at", "dem", "elevation", "feature", "geoname_id", "latitude", "longitude", "modified_on", "name", "population", "timezone", "updated_at") VALUES ('admin1_07', 'admin2_05', 'admin3_03', 'admin4_01', 'Geoname_ascii23', 'CM', '2014-12-09 09:46:31.163858', 380, 74, 'AADM4', 23, 8.34005686792595, 17.6904713901371, '2014-09-13', 'Geoname023', 67372, 'Time/Zone23', '2014-12-09 09:46:31.163858') RETURNING "geoname_id" INSERT INTO "geonames" ("admin1", "admin2", "admin3", "admin4", "asciiname", "country_code", "created_at", "dem", "elevation", "feature", "geoname_id", "latitude", "longitude", "modified_on", "name", "population", "timezone", "updated_at") VALUES ('admin1_07', 'admin2_05', 'admin3_03', 'admin4_02', 'Geoname_ascii24', 'CM', '2014-12-09 09:46:31.166584', 202, 104, 'AADM4', 24, 83.1516503438357, 95.2018051676951, '2014-11-06', 'Geoname024', 96826, 'Time/Zone24', '2014-12-09 09:46:31.166584') RETURNING "geoname_id" INSERT INTO "geoname_names" ("created_at", "geoname_id", "language", "name", "name_id", "updated_at") VALUES ('2014-12-09 09:46:31.168885', 23, 'en', 'GeonameName #4', 4, '2014-12-09 09:46:31.168885') RETURNING "name_id" SELECT geonames.geoname_id, geonames.asciiname, geonames.latitude, geonames.longitude, geonames.feature, geonames.country_code, geonames.admin1, geonames.admin2, geonames.admin3, geonames.admin4, geonames.population, geonames.elevation, geonames.dem, geonames.timezone, geonames.modified_on, geonames.created_at, geonames.updated_at, CASE WHEN geoname_names.name IS NOT NULL THEN geoname_names.name ELSE geonames.name END AS name FROM "geonames" LEFT OUTER JOIN geoname_names ON geoname_names.geoname_id = geonames.geoname_id AND geoname_names.name_id IN (SELECT name_id FROM geoname_names WHERE geoname_names.geoname_id = geonames.geoname_id AND geoname_names.language = 'en' ORDER BY geoname_names.short DESC, geoname_names.preffered DESC, geoname_names.colloquial ASC, geoname_names.historic ASC LIMIT 1 ) WHERE "geonames"."country_code" = 'CM' AND "geonames"."feature" = 'AADM4' AND "geonames"."admin1" = 'admin1_07' AND "geonames"."admin2" = 'admin2_05' AND "geonames"."admin3" = 'admin3_03' ORDER BY CASE WHEN geoname_names.name IS NOT NULL THEN geoname_names.name ELSE geonames.name END LIMIT 1