In the query below, I would like to create two new variables: one variable derived from postcode, another derived from area (which is derived from postcode).
select postcode,
case when substring(postcode for 2) = 'YO' then 'York',
when substring(postcode for 1) = 'E' then 'London E'
...
else 'unknown'
end as area,
case when area in ('York', 'London E') then 'England'
when area = 'Belfast' then 'Northern Ireland'
else 'unknown'
end as country
from countries
group by area, country
Initial output:
postcode
SE14 5XG
EX1 3AL
E2 0QP
NW7 2NT
Gu2 8nj
Expected output:
postcode area country
SE14 5XG London SE England
EX1 3AL Exeter England
...
NW7 2NT London NW England
Gu2 8nj Guildford England
The code above returns "error: column "area" does not exist"
in PostGreSQL.