db.query(
`
SELECT
Nationality
, MAX(nationalitySearch.value)
, MAX(CASE WHEN nationalitySearch.value IS NOT NULL THEN true ELSE false END) AS Searched
, MAX(CASE WHEN nationality.value IS NOT NULL THEN true ELSE false END) AS Selected
, MAX(CASE WHEN displayName.value IS NOT NULL THEN true ELSE false END) AS Possible
FROM artists AS a
LEFT OUTER JOIN default_state as displayName
ON displayName.table = 'artists'
AND displayName.field = 'DisplayName'
AND a.DisplayName = displayName.value
AND displayName.type = 'in'
LEFT OUTER JOIN default_state as nationality
ON nationality.table = 'artists'
AND nationality.field = 'Nationality'
AND a.Nationality = nationality.value
AND nationality.type = 'in'
LEFT OUTER JOIN default_state AS nationalitySearch
ON nationalitySearch.table = 'artists'
AND nationalitySearch.field = 'Nationality'
AND nationalitySearch.type = 'like'
AND a.Nationality ILIKE CONCAT('%', nationalitySearch.value, '%')
GROUP BY Nationality
ORDER BY
2 DESC
, 3 DESC
, 4 DESC
, 1
`
)