ORDER BY a concatenated name in Oracle?
I have a table called USERS from which I'm getting an employee's first and last name, and printing them out as one name. How do I use ORDER BY correctly on this data?
Here's what I have:
SELECT l.OF开发者_开发问答FICE_NAME, (us.LAST_NAME || ' , ' || us.FIRST_NAME) AS "Employee Name"
FROM LOCAL_OFFICE l, USERS us
WHERE l.LOCAL_OFFICE_ID = us.LOCAL_OFFICE_ID
ORDER BY l.OFFICE_NAME --what do I place here?--
I will do this through a JOIN instead of the more expensive FROM, but how do I order by the name alphabetically?
You can say:
ORDER BY office_name, "Employee Name"
That works in 11G, but maybe not in older version of Oracle.
ORDER BY l.OFFICE_NAME, (us.LAST_NAME || ' , ' || us.FIRST_NAME)
As your concatenation isn't really adding anything new, why not just
ORDER BY l.OFFICE_NAME, us.LAST_NAME, us.FIRST_NAME
Edit: If there are indexes on USERS.LAST_NAME
and / or USERS.FIRST_NAME
, this will be faster than ordering by a concatenation result
You can use:
ORDER BY 1,2
This is not fantastically good practice, but it works.
精彩评论