View previous topic :: View next topic
|
Author |
Message |
Parasakthi Currently Banned New User
Joined: 14 Feb 2008 Posts: 1 Location: Bangalore
|
|
|
|
Hi all,
Where we have to use order by 1,2 clause?
What is the difference between order by column name and order by(nos).. |
|
Back to top |
|
|
vasanthkumarhb
Active User
Joined: 06 Sep 2007 Posts: 275 Location: Bang,iflex
|
|
|
|
Hi......
GROUP BY class concentrates on fetching data row wise, where as ORDER BY specifies fetching the data in column wise.
consider one example query for analysis.
SELECT DEPT,COUNT(*) FROM STAFF GROUP BY DEPT HAVING MAX(COMM)>5000 ORDER BY 2 DESC
Here query considering on the 2nd column that is DEPT and the maximum of commission and grouped together after satisfying HAVING condition.
If u want to operate on the particular column or particular group u can give ORDER BY 2(2nd column) DESC or GROUP BY 3(3rd row) ASC(default) |
|
Back to top |
|
|
stodolas
Active Member
Joined: 13 Jun 2007 Posts: 632 Location: Wisconsin
|
|
|
|
Did you even read the O/P? They didn't ask anything about GROUP BY.
Parasakthi: The syntax of ORDER BY allows you to refer to columns either by name or by position. Both are valid references and give them same end result. |
|
Back to top |
|
|
Phrzby Phil
Senior Member
Joined: 31 Oct 2006 Posts: 1042 Location: Richmond, Virginia
|
|
|
|
But of course the danger in using numbers is that they must be adjusted if someone later modifies the SELECT list, and names are more meangful.
However, if you have a bunch of UNIONs, the col names most likely differ, so I think you must use numbers. |
|
Back to top |
|
|
acevedo
Active User
Joined: 11 May 2005 Posts: 344 Location: Spain
|
|
|
|
but sometimes you need to use NUMBERS becouse the field you need to order by is not a column but a function, for example, column3+column4-column5... |
|
Back to top |
|
|
Phrzby Phil
Senior Member
Joined: 31 Oct 2006 Posts: 1042 Location: Richmond, Virginia
|
|
|
|
Can't you give a name to a SELECT expression? |
|
Back to top |
|
|
acevedo
Active User
Joined: 11 May 2005 Posts: 344 Location: Spain
|
|
|
|
I cannot test it now BUT I think you cannot use that name in order by... can anyone try it? |
|
Back to top |
|
|
|