I’m using Oracle. My sql returns a column like this
Name:
John Smith
David Lee
…
If I do Order by Name, it will order by first name. How do I order by Last name? If I do Order by Lastname, Firstname oracle returns invalid identifiers. I tried substr, instr but it doesn’t work. I know the sql is tedious but just want the data to quickly fix this issue.
Full SQL:
http://pastebin.com/hYkdHBDM
First, in order to sort by LastName, it needs to be one of the columns you return in each of the queries in your Union All. Second, you can greatly simplify your query by using a common-table expression. Third, do not use the comma delimited syntax for Joins (e.g. From TableA, TableB, TableC…). Instead use the ISO Join syntax.
Btw, the last query will probably have a problem in that it did not have a Group By (which I denoted with
Group By ???) but you are using aggregate functions.