Click here to Skip to main content
12,248,978 members (35,400 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: TSQL
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 16-Oct-12 7:42am
Edited 16-Oct-12 11:25am
Maciej Los218.2K
v3
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

It is good that you are friends with your instructor. Smile | :)
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.
  Permalink  
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!
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Marcus answer is very good!

To "sort a table" you need to use query like this:
SELECT Region, Contact
FROM YourTable
ORDER BY Region, Contact
  Permalink  
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160426.1 | Last Updated 16 Oct 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100