aspose file tools*
The moose likes JDBC and the fly likes Help with SQL Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Help with SQL" Watch "Help with SQL" New topic
Author

Help with SQL

Sobha periasamy
Greenhorn

Joined: Apr 10, 2005
Posts: 2
Hi All,
I need some help with SQL Queries. I want to sort the rows in a table by three columns. The Sorting order can be ascending or descending and can be specified for each of the three columns. I think in SQL we can sort either all the columns ascending or all the columns descending. But is there
a way where I can specify sorting order for each of the column. I am not sure whether I posted the question in a right forum, but still I am hoping to get an answer for my problem.

Thanks,
Sobha.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336



JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Dave Wingate
Ranch Hand

Joined: Mar 26, 2002
Posts: 262
This is probably not the most elegant solution, but you could divide your three problems (i.e. sorting by three different columns) into three different select statements.

Suppose we have columns A, B & C in table MYTABLE.

One way to select these columns and sort with them is to use:

SELECT * FROM MYTABLE ORDER BY A, B, C

But it's also valid to say:

SELECT * FROM (
SELECT * FROM (
SELECT * FROM MYTABLE ORDER BY C
) ORDER BY B
) ORDER BY A


I haven't tried this out on an example DB, but I think it should work.


Fun programming etcetera!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61653
    
  67

"sobha",

There aren't many rules that you need to worry about here on the Ranch, but one that we take very seriously regards the use of proper names. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Thanks!
bear
Forum Bartender


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Sunil Dumpala
Greenhorn

Joined: Jul 28, 2005
Posts: 29
Dave,

How is your query different from Paul's? Is your query better in performance perspective. Could you please explain. Just eager to know.

Thanks,
Sunil Dumpala
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Help with SQL