3

I am trying to do this:

select first_name || ' ' || last_name as full_name, Length(full_name) as len from customer order by len

It is not possible;

column "full_name" does not exist

So, I have to do this:

select first_name || ' ' || last_name as full_name, Length(first_name || ' ' || last_name) as len from customer order by len

Does it mean sql engine has to compute expression 'first_name || ' ' || last_name' two times?

1 Answer 1

3

As you observe, what you want to do is not possible. Instead, you can use a lateral join to calculate values in the FROM clause:

select v.full_name, Length(v.full_name) as len
from customer c cross join lateral
     (values (first_name || ' ' || last_name)
     ) v(full_name)
order by len;
Sign up to request clarification or add additional context in comments.

2 Comments

Does it mean we can't pass aliases to functions()?
@Mandroid . . . No, it means that you cannot re-use an alias in the same select (or where) that defines it. You could use a CTE or subquery as well.

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.