Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I need to reorder a list in a table for a friend using a SELECT statement

The table looks like this:
Region    Contact
---------------------
Cape Town        Fred
Cape Town        Joe
Cape Town        Anna
Durban           John
Durban           Mary
Johannesburg     Frank


It needs to look like this:
Region    Contact
---------------------
Durban          John
Durban          Mary
Johannesburg    Frank
Cape Town       Anna
Cape Town       Fred
Cape Town       Joe

Is this even possible to do as Order By would obviously not work?
Posted
Updated 16-Oct-12 10:25am
v3

It is good that you are friends with your instructor. :)
Since you have no logical basis for ordering the data then you are correct that an order by will not work, unless there is another column in the table that you have neglected to show us.
You could examine the UNION statement as a possible way to do this, though.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 16-Oct-12 13:10pm    
A 5. It's worth noting that, conceptually, from the standpoint of a user of SQL, a table does not have certain order, so nothing could be re-ordered in the table. The order appears only in a data set obtained via a query.
--SA
Maciej Los 16-Oct-12 16:20pm    
Agree, +5!
Marcus answer is very good!

To "sort a table" you need to use query like this:
SQL
SELECT Region, Contact
FROM YourTable
ORDER BY Region, Contact
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 16-Oct-12 16:43pm    
Apparently, ORDER BY, a 5.
(I would like to know the motivation of the one voted 1...)
--SA
Maciej Los 16-Oct-12 16:45pm    
Sometimes it happens ;(
Anyway, thank you, Sergey ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900