with initial_table as (
select *
from elevations
order by lat, lon
),
the_lons as (
select
row_number() over (order by lon) as lon_number,
lon
from (select distinct lon from elevations order by lon)
),
the_lats as (
select
row_number() over (order by lat) as lat_number,
lat
from (select distinct lat from elevations order by lat)
),
the_lon_lats as (
select *
from the_lons
cross join the_lats
),
with_lons as (
select *
from elevations
left join the_lons
using (lon)
),
with_lon_lat as (
select *
from with_lons
left join the_lats
using (lat)
)
select *
from the_lon_lats
left join with_lon_lat
using (lon_number, lat_number)
order by lat_number, lon_number