![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
#3
| |||
| |||
|
|
Take a look at the following example: table T(i INTEGER, j INTEGER) I want to get the value of i where j is minimal and some conditions apply. (1) SELECT i FROM T WHERE [condition] AND j IN (SELECT min(j) FROM T WHERE [condition]) The best would be if such a syntax was allowed (which it isn't): SELECT i, min(j) FROM T WHERE [condition] How can I rewrite the SELECT statement in (1) such that [condition] doesn't have to be typed in twice? Or how can I achieve the same thing in some other way? (I am open to other solutions as well as long as they are inside the limits of Oracle PL/SQL.) Will this do what you want? |
#4
| |||
| |||
|
|
"Agoston Bejo" <gusz1 (AT) freemail (DOT) hu> wrote: Take a look at the following example: table T(i INTEGER, j INTEGER) I want to get the value of i where j is minimal and some conditions apply. (1) SELECT i FROM T WHERE [condition] AND j IN (SELECT min(j) FROM T WHERE [condition]) The best would be if such a syntax was allowed (which it isn't): SELECT i, min(j) FROM T WHERE [condition] How can I rewrite the SELECT statement in (1) such that [condition] doesn't have to be typed in twice? Or how can I achieve the same thing in some other way? (I am open to other solutions as well as long as they are inside the limits of Oracle PL/SQL.) Will this do what you want? Select i.min(j) from T where [condition]. GROUP BY i; No it won't I now realize..It will return all i and, for each i, the Min(j) - the OP wants only those i that have as j the |
#5
| |||
| |||
|
|
Take a look at the following example: table T(i INTEGER, j INTEGER) I want to get the value of i where j is minimal and some conditions apply. (1) SELECT i FROM T WHERE [condition] AND j IN (SELECT min(j) FROM T WHERE [condition]) The best would be if such a syntax was allowed (which it isn't): SELECT i, min(j) FROM T WHERE [condition] How can I rewrite the SELECT statement in (1) such that [condition] doesn't have to be typed in twice? Or how can I achieve the same thing in some other way? (I am open to other solutions as well as long as they are inside the limits of Oracle PL/SQL.) |
#6
| |||
| |||
|
|
Actually, here is what I could come up with: select i from (SELECT i, j FROM T WHERE [condition] ORDER BY j ) WHERE ROWNUM = 1 Is this very inefficient? |
#7
| |||
| |||
|
|
"Agoston Bejo" <gusz1 (AT) freemail (DOT) hu> wrote Actually, here is what I could come up with: select i from (SELECT i, j FROM T WHERE [condition] ORDER BY j ) WHERE ROWNUM = 1 Is this very inefficient? According to oracle doc the rownum is computed before the "order by" statement is applied. |
|
Perhaps better: select i,j from (select i,j,row_number() over (order by j) rn) where rn=1; Not without additional input for the optimizer: |
#8
| |||
| |||
|
|
baer@DEMO10G>select i from test where j=0; I ---------- 100 |
| Note the appearence of STOPKEY: The optimizer nows that with rownum=XY you will likely not take all of the result. This might lead the optimizer to favour a first_rows plan. Strange: costs and execution time seems bo be equal, even for big |
#9
| |||
| |||
|
|
Strange: costs and execution time seems bo be equal, even for big tables. Not in my tests. The difference was small, but analytics always came last |
|
I thought the optimizer is guessing disk and cpu usage. But they won't make any difference. A full scan is a fullscan, no matter |
|
One advantage of the solution with analytic functions, is that you can do: select * from ( select id,to_char(datetime,'yyyy-mm-dd hh24:mi:ss'),row_number() over (order by id) rn from fdetailrecord ) where rn between 5 and 10; |
![]() |
| Thread Tools | |
| Display Modes | |
| |