0

I have two columns of x,y coordinates in a table on a Postgres database.

I can find the extremes in x and y using:

select min(x), max(x), min(y), max(y)
from coords

How do assign these to variables something like this:

select min(x) as xmin, max(x) as xmax, min(y) as ymin, max(y) as ymax
from coords

to achieve

select y
from coords
where x = xmin

etc...to obtain the four extreme points in the half-billion row dataset? (the point of the exercise)

the desired select statement works but I can't use the "where" clause as it says that xmin is not a column. What is the correct approach, or in the unlikely event that I am using the correct approach, what is the correct syntax?

1 Answer 1

3

One way is with a join and subquery:

select c.*
from coords c join
     (select min(x) as xmin, max(x) as xmax, min(y) as ymin, max(y) as ymax
      from coords
     ) clim
     on c.x in (clim.xmin, clim.xmax) or c.y in (clim.ymin, clim.ymax)

Another way to do this is with window functions:

select c.*
from (select c.*,
             least(row_number() over (order by x),
                   row_number() over (order by x desc),
                   row_number() over (order by y),
                   row_number() over (order by y desc)
                  ) as seqnum
      from coords c
     ) c
where seqnum = 1

And another way, if you really want just 4 points is:

select * from coords order by x limit 1 union all
select * from coords order by x desc limit 1 union all
select * from coords order by y limit 1 union all
select * from coords order by y desc limit 1

With such a large number of rows, these will all run faster if you have indexes on x and y. In this case, the last is probably the fastest.

Sign up to request clarification or add additional context in comments.

1 Comment

Lastest is fastest - 600-640 ms per statement.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.