I am currently working on a query from the citiesXXXX table in the geonames DB which displays the alternate names of places in other languages, i.e. 'en', 'de', 'fr' etc.
While I managed to create a pivot table by JOINing the AlternateNames table, I am having a hard time generating a condition if e.g. there are two records for one city for the 'de' language, and one of these is marked with TRUE in the 'isPreferredName' column.
My current query:
SELECT
c.geonameid,
c.name,
MAX(CASE
WHEN an.isoLanguage = '' THEN an.alternateName
END) AS 'ALT',
MAX(CASE
WHEN an.isoLanguage = 'en' THEN an.alternateName
END) AS 'EN',
MAX(CASE
WHEN an.isoLanguage = 'fr' THEN an.alternateName
END) AS 'FR',
c.country
FROM
cities15000 c
JOIN
alternatenames an USING (geonameid)
GROUP BY c.name
ORDER BY c.country , c.name
Ideally, I would like to fetch the record marked as 'isPreferredName' for each language, if there is one, but otherwise simply the record corresponding to the language.
I have been looking for similar questions, here and elsewhere, but all questions on nested conditions deal with numerical values, to be fed into one formula, rather than another. I don't need any operation to be performed on the output.
As requested, here is the SHOW CREATE TABLE for both tables involved:
| alternatenames |
CREATE TABLE `alternatenames` (
`alternatenameid` int NOT NULL COMMENT 'ID alternate name',
`geonameid` bigint DEFAULT NULL COMMENT 'GeoName ID',
`isoLanguage` varchar(7) DEFAULT NULL COMMENT 'ISO639 2-3 char, ''post''=Postal Code, ''iata'',''icao'' & faac=Airport Code, ''fr_1793'', ''abbr''=abbreviation, ''link''=URL, ''wkdt''=wikidata',
`alternateName` varchar(400) DEFAULT NULL COMMENT 'Alt name or variant',
`isPreferredName` tinyint(1) DEFAULT NULL COMMENT 'Alternate name is an official/preferred name',
`isShortName` tinyint(1) DEFAULT NULL COMMENT 'Short name like ''California'' for ''State of California',
`isColloquial` tinyint(1) DEFAULT NULL COMMENT 'Colloquial or slang term. Example: ''Big Apple'' for ''New York''',
`isHistoric` tinyint(1) DEFAULT NULL COMMENT 'Historic and was used in the past. Example ''Bombay'' for ''Mumbai''',
PRIMARY KEY (`alternatenameid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
| cities15000 |
CREATE TABLE `cities15000` (
`geonameid` bigint NOT NULL COMMENT 'Integer id of record in geonames database',
`name` varchar(200) DEFAULT NULL COMMENT 'Name of geographical point',
`asciiname` varchar(200) DEFAULT NULL COMMENT 'Name of geographical point in plain ASCII characters',
`alternatenames` varchar(10000) DEFAULT NULL COMMENT 'Comma separated, ASCII names automatically transliterated, convenience attribute from alternatename table',
`latitude` decimal(10,7) DEFAULT NULL COMMENT 'Latitude in decimal degrees (wgs84)',
`longitude` decimal(10,7) DEFAULT NULL COMMENT 'Longitude in decimal degrees (wgs84)',
`fclass` char(1) DEFAULT NULL COMMENT 'http://www.geonames.org/export/codes.html (featureCodes.txt)',
`fcode` varchar(10) DEFAULT NULL COMMENT 'http://www.geonames.org/export/codes.html (featureCodes.txt)',
`country` varchar(2) DEFAULT NULL COMMENT 'ISO-3166 2-letter country code',
`cc2` varchar(200) DEFAULT NULL COMMENT 'Alternate country codes, comma separated, ISO-3166 2-letter country code',
`admin1` varchar(20) DEFAULT NULL COMMENT '1st Level Admin Division - FIPS for US (admin1Codes.txt)',
`admin2` varchar(80) DEFAULT '' COMMENT '2nd Level Admin Division - County in US (admin2Codes.txt',
`admin3` varchar(20) DEFAULT '' COMMENT '3rd Level Admin Division',
`admin4` varchar(20) DEFAULT '' COMMENT '4th Level Admin Division',
`population` bigint DEFAULT NULL COMMENT 'Population (8 Byte INT)',
`elevation` int DEFAULT '0' COMMENT 'Digital elevation model (srtm3)',
`gtopo30` int DEFAULT '0' COMMENT 'Digital elevation model (gtopo30)',
`timezone` varchar(40) DEFAULT NULL COMMENT 'IANA timezone id (timeZone.txt)',
`moddate` date DEFAULT NULL COMMENT 'Date of last modification in yyyy-MM-dd format',
PRIMARY KEY (`geonameid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
(N.B. There are different tables beginning with 'cities' in the DB, but all have an identical structure.)