Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
Home -> Community -> Usenet -> c.d.o.server -> Using ROWNUM with ORDER BY clause with a CASE statement
I know how to grab records 10 - 20 of a 100 row resultset using
ROWNUM, but now I've complicated things a bit. I have a rule in the
ORDER BY that brings any record that has "XXX" at the end of one
particular field's value to appear first in the result set. The
problem is, the ROWNUM for some of these records may be greater than
the ROWNUM value I've specified. Let's say the record I want to
appear happens to have a ROWNUM of 69, so it should appear at (along
with other rows that qualify) at the top of the resultset in my 10 -
20 range, but since I am asking for ROWNUM BETWEEN 10 AND 20, the row
with ROWNUM of 69 gets left out. How can I get this row to be
included?
Let me preface this with the fact that we are not to use stored
procedures. I have to do this with bare-ass SQL. Here is the SQL
that I have so far.
Select * FROM ( Select SWIFT_BANKS.*, ROWNUM RN FROM SWIFT_BANKS
WHERE SWIFT_ID LIKE 'HABBPKKA%' ORDER BY (CASE SUBSTR(SWIFT_ID,9,3) WHEN
'XXX' THEN 1 ELSE ASCII(SWIFT_ID) END) )
WHERE RN BETWEEN 1 AND 21
It returns 69 records of which I choose the first 20.
If I run just this part:
Select * FROM ( Select SWIFT_BANKS.*, ROWNUM RN FROM SWIFT_BANKS
WHERE SWIFT_ID LIKE 'HABBPKKA%' ORDER BY (CASE SUBSTR(SWIFT_ID,9,3) WHEN