Copy link to clipboard
Copied
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 | City2 | Rate1 | Rate2 |
---|---|---|---|
Dallas | Mentor | 100 | 65 |
Richmond | New York | 95 | 60 |
Miame | Houston | 125 | 116 |
Enid | Denver | 40 | 38 |
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
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
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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