SQL query union and sorting
Posted on 2012-08-24
I have a massive union query that I need to sort by each individual part. I can’t seem to get it to work, though, even using the UNION ALL. Basically (in English, not SQL, for simplicity):
Select * from TableA ORDER BY LastName
Select * from TableB ORDER BY Employee
Select * FROM TableC ORDER BY Hostess
The only way to get that to work is to stick in an ORDER BY 8 at the end of the query, but each query needs to have different sort criteria, as it displays in different tabs in our smartphone app.
Now… we’ve tried the UNION ALL and the ALIAS option, but that didn’t work, either. It did change the way the data came up in the result set, but it still didn’t sort on LastName, say.
Any insight? This will need to work for access and SQL Server.