0

I have a rows with (firstname, lastname, phone, email). What I want to make is a query which sort first by concat(firstname, lastname) asc and the rest rows, where the firstname and lastname are null have to come after the sorted by concat(firstname, lastname) ascand to be sorted depending on phone/email (if phone is null sort by email, if email is null sort by phone). Any suggestion can help me?

2
  • 1
    look for case, coalesce Commented Jul 7, 2016 at 12:01
  • Note that case/coalesce require compatible data types. Commented Jul 7, 2016 at 12:04

2 Answers 2

3

You can put these rules into the order by. I think these are:

order by (firstname is null and lastname is null) asc,
         concat(firstname, lastname),
         coalesce(phone, email)

I am not sure if the first condition should be and or or.

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

Comments

1
select * from table order by concat(firstname, lastname) asc,  (firstname is null and lastname is null) asc, coalesce(phone, email)

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.