| Author |
Message |
mf_user Currently Banned Active User
Joined: 05 Jan 2006 Posts: 76
|
|
|
|
Hi,
I was just going through a program and found that a SELECT query with ORDER BY. This is an embedded query in a COBOL-DB2 program.
This particular query has a SELECT statment with four columns from DB2 table and the ORDER BY is having a different column name (not from the four column names of SELECT). Is it possible?
Can we have such a query? I read that if a column has to be used in ORDER BY then it must be mentioned in SELECT also.
Please explain.
Thanks. |
|
| Back to top |
|
 |
References
|
Posted: Mon Nov 26, 2007 6:43 pm Post subject: Re: why a DB2 column used in ORDER BY is mandatory in SELECT too |
 |
|
|
 |
muthuvel
Active User
Joined: 29 Nov 2005 Posts: 178 Location: Chennai
|
|
|
|
| Quote: |
| Can we have such a query? I read that if a column has to be used in ORDER BY then it must be mentioned in SELECT also. |
Not So.It is enought that if the columns are present in the table which are given in the SQL
This is what the manual says for your question
| Quote: |
| If the column name is not found in the result table, it must unambiguously identify a column of a table, view, or nested table expression in the FROM clause of the subselect. |
Correct me if i am wrong. |
|
| Back to top |
|
 |
mf_user Currently Banned Active User
Joined: 05 Jan 2006 Posts: 76
|
|
|
|
| Sounds interesting. Any more thoughts on this? |
|
| Back to top |
|
 |
Craq Giegerich
Senior Member
Joined: 19 May 2007 Posts: 709 Location: Virginia, USA
|
|
|
|
| mf_user wrote: |
| Sounds interesting. Any more thoughts on this? |
I think it used to be required but not anymore, not sure when the change was made. |
|
| Back to top |
|
 |
Anuj D.
Senior Member
Joined: 22 Apr 2006 Posts: 1124 Location: Mumbai, India
|
|
|
|
Hi,
Yeah is should be this way only...
| Quote: |
| I think it used to be required but not anymore, not sure when the change was made. |
When one writes a query then he is creating a 'view' of 'that' table for which the query is written..so even by intuition one would like to "order" that 'view' on the basis of some column in that 'view' ..not on some arbitrary column from the base table. |
|
| Back to top |
|
 |
dbzTHEdinosauer
Senior Member
Joined: 20 Oct 2006 Posts: 903 Location: germany
|
|
|
|
| Quote: |
Yeah is should be this way only...
|
so even though the column is not necessary, you want it there anyway? That is not efficient. The proper way would be to document the procedure so that the results are anticipated. |
|
| Back to top |
|
 |
|
|