0

I am trying to do the following in Postgresql

SELECT * FROM table_one
INNER JOIN table_two ON table_one.id = table_two.table_one_id
WHERE (table_one.some_value > length('((-1,0),(table_two.x,table_two.y))'::lseg))

Obviously this doesn't work so my question is how do you do something like this?

'((-1,0),(%,%))'::lseg , table_two.x , table_two.y

or

'((-1,0),('|| table_two.x ||',('|| table_one.x || '))'::lseg

2
  • The second one should work… What happened when you tried it? Commented Nov 26, 2013 at 19:32
  • I am getting this ERROR: invalid input syntax for type lseg also the x and y columns are floats Commented Nov 26, 2013 at 19:45

1 Answer 1

1

You could make your second suggestion work by bracketing it more clearly - casting takes precedence to concatenation so it is trying to cast '))' to an lseg. If you bracket the concatenation before casting it the statement will work ...

length(('((-1,0),('|| table_two.x ||','|| table_two.y || '))')::lseg)

But it would be neater to use the fields values in type conversion functions. At least that way the error message would make more sense if there were any.

In this case define the lseg with two points. Build one of the points is built from a literal and one from the fields in a table.

length(lseg('(-1,0)'::point, point(table_two.x, table_two.y)))

Or both from numeric values, which I think looks best and also happens to be shortest

length(lseg(point(-1,0), point(table_two.x, table_two.y)))
Sign up to request clarification or add additional context in comments.

Comments

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.