Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help with SQL

 
Sobha periasamy
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Dave Wingate
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64708
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
 
Sunil Dumpala
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic