![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
Is there a simple way to use a previous result in an ORDER BY clause? I have a rather complicate query that filters, sorts and returns a series of IDs: FOO_ID ====== 1 98 12 33 Then, I use these IDs to fetch further information about the items they represent: SELECT ....... FROM FOO LEFT JOIN BAR ....... WHERE FOO_ID IN (1, 98, 12, 33) I keep the two queries separate to avoid excessive complexity. I compose the SQL code using PHP. Right now, the second query comes unsorted from Oracle: I use PHP to sort it at a later stage in my application (my PHP skills are better than my SQL ones). Would it be possible to use the ID list to sort the second query inside Oracle? Server runs Oracle9i Enterprise Edition Release 9.2.0.1.0 Thank you in advance. -- --http://alvaro.es- Álvaro G. Vicario - Burgos, Spain -- Mi sitio sobre programación web:http://bits.demogracia.com -- Mi web de humor al baño María:http://www.demogracia.com -- Server runs Oracle9i Enterprise Edition Release 9.2.0.1.0 |
|
I keep the two queries separate to avoid excessive complexity. I compose the SQL code using PHP. |
|
Would it be possible to use the ID list to sort the second query inside Oracle? |
#3
| |||
| |||
|
|
Is there a simple way to use a previous result in an ORDER BY clause? I have a rather complicate query that filters, sorts and returns a series of IDs: FOO_ID ====== 1 98 12 33 Then, I use these IDs to fetch further information about the items they represent: SELECT ....... FROM FOO LEFT JOIN BAR ....... WHERE FOO_ID IN (1, 98, 12, 33) I keep the two queries separate to avoid excessive complexity. I compose the SQL code using PHP. Right now, the second query comes unsorted from Oracle: I use PHP to sort it at a later stage in my application (my PHP skills are better than my SQL ones). Would it be possible to use the ID list to sort the second query inside Oracle? Server runs Oracle9i Enterprise Edition Release 9.2.0.1.0 Dessuported-unpatched DB version. |
|
I keep the two queries separate to avoid excessive complexity. I compose the SQL code using PHP. Wrong design: better one query that does all the work. |
|
Would it be possible to use the ID list to sort the second query inside Oracle? Yes. You should extract the ID and the order you want and re-write the second query to use an ORDER BY clause on the 'order' column. SELECT ....... FROM FOO a LEFT JOIN BAR b....... ON a.FOO_ID = b.FOO_ID ORDER BY b.MY_ORDER |
#4
| |||
| |||
|
#5
| |||
| |||
|
|
Is there a simple way to use a previous result in an ORDER BY clause? I have a rather complicate query that filters, sorts and returns a series of IDs: FOO_ID ====== * * * 1 * * *98 * * *12 * * *33 Then, I use these IDs to fetch further information about the items they represent: SELECT ....... FROM FOO LEFT JOIN BAR ....... WHERE FOO_ID IN (1, 98, 12, 33) I keep the two queries separate to avoid excessive complexity. I compose the SQL code using PHP. Right now, the second query comes unsorted from Oracle: I use PHP to sort it at a later stage in my application (my PHP skills are better than my SQL ones). Would it be possible to use the ID list to sort the second query inside Oracle? Server runs Oracle9i Enterprise Edition Release 9.2.0.1.0 Thank you in advance. -- --http://alvaro.es- Álvaro G. Vicario - Burgos, Spain |
#6
| |||
| |||
|
|
SELECT ....... FROM * FOO, * BAR WHERE * FOO.FOO_ID=BAR.FOO_ID(+) * AND FOO_ID IN (1, 98, 12, 33) ORDER BY * INSTR('1,98,12,33,' , TO_CHAR(FOO_ID)||','); Before deciding to use the above technique, determine if there is a better way to do *everything* in a single SQL statement. *You might be able to do this by wrapping your complicated SQL statement into an inline view, and joining to that just as if it were a regular table: SELECT ....... FROM * FOO, * BAR, * ( complicated SQL here ) V WHERE * V.FOO_ID=FOO.ID * AND FOO.FOO_ID=BAR.FOO_ID(+) ORDER BY * V.RN; The RN column would be generated inside the inline view V, possibly like this, if there is an ORDER BY clause in the inline view: * ROWNUM RN |
![]() |
| Thread Tools | |
| Display Modes | |
| |