Skip to main content
Inspiring
September 16, 2009
Answered

Query Question

  • September 16, 2009
  • 2 replies
  • 1268 views

I have a table with 4 columns I want to sort and not sure how.

I can sort by 1 column but I need the sort to be on 2 columns.

Here are the 4 columns

City1
City2Rate1Rate2
DallasMentor10065
RichmondNew York9560
MiameHouston125116
EnidDenver4038

I want to sort them using the UNION of the values in the RATE1 and RATE2 columns

So I want them sorted like this

Miame 125

Houston 116

Dallas  100

Richmond 95

Mentor 65

New York 60

Enid  40

Denver 38

Any Help

This topic has been closed for replies.
Correct answer Cyril Hanquez

weird table IMO. Well, as already said you didn't specify which DB you are using but in Oracle you can try:

SELECT City1,

              Rate1

  FROM  myTable

UNION

SELECT City2,

              Rate2

  FROM  myTable

ORDER BY 2 DESC

2 replies

Cyril Hanquez
Cyril HanquezCorrect answer
Participating Frequently
September 25, 2009

weird table IMO. Well, as already said you didn't specify which DB you are using but in Oracle you can try:

SELECT City1,

              Rate1

  FROM  myTable

UNION

SELECT City2,

              Rate2

  FROM  myTable

ORDER BY 2 DESC

Inspiring
September 16, 2009

SQL questions are probably best asked on an SQL forum, but no matter.

It might help to read the docs for your DB of choice, because this will all be covered there (reading the docs is always a good way to find out how to do something, btw ;-)

Anyway, the piece of the puzzle that you are missing here is that one can specify more than one column in an ORDER BY statement.

I can't point you to the relevant docs because you don't say which DB system you're using, but it'll be in there, as well as examples and what not.

--

Adam