Solved

# rownum inbetween

Posted on 2014-03-13
336 Views
I am using following query but rownum in between function is not working

``````SELECT p.fldstring folder_level_1,
p2.fldstring folder_level_2,
p3.fldstring folder_level3,
d.doc_name
FROM document d
INNER JOIN folder f ON d.id = f.lid
LEFT JOIN PATH p ON f.foldid = p.fldtype || '-' || p.fldkey
LEFT JOIN PATH p2 ON p2.fldkey = p.fldkey2
LEFT JOIN PATH p3 ON p3.fldkey = p2.fldkey2
where rownum = 14127 and rownum = 54127;
``````
0
Question by:CalmSoul
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 143

Accepted Solution

Guy Hengel [angelIII / a3] earned 167 total points
ID: 39927569
Yes that is known behavior of ROWNUM as its actually evaluated after the query run.
You must put it into a subquery and filter in the outer query
0

LVL 32

Assisted Solution

awking00 earned 167 total points
ID: 39927623
Rownum is a pseudo column that assigns a value to a record after the query is actually executed so you can never return records where the rownum = anything (other than 1). For example, select * from anytable where rownum = 2 will not return anything because the first record would receive a rownum value of 1 and, since it must equal 2, it doesn't retrieve it and when the second record gets retrieved it also would receive a rownum value of one and not retrieve it, and so on, never retrieving a record even if there are many rows in the table. Additionally, selecting rownum from a inner join b left join c left join d left join e will produce rownum values that are different from selecting rownum from a inner join b left join e left join d left join c. You need to determine some criteria for the rows that you want to retrieve other than rownum. Perhaps you can provide some sample data for each of the tables and what you want to see as output.
0

LVL 28

Assisted Solution

Naveen Kumar earned 166 total points
ID: 39928336
that will not work as already mentioned above.. you can try :

select * from (
SELECT p.fldstring folder_level_1,
p2.fldstring folder_level_2,
p3.fldstring folder_level3,
d.doc_name, rownum rr
FROM document d
INNER JOIN folder f ON d.id = f.lid
LEFT JOIN PATH p ON f.foldid = p.fldtype || '-' || p.fldkey
LEFT JOIN PATH p2 ON p2.fldkey = p.fldkey2
LEFT JOIN PATH p3 ON p3.fldkey = p2.fldkey2 )
where rr = 2 and rr = 3 ;

-- i am trying to get the 2nd and the 3rd record from the output in the where clause.

Thanks
0

LVL 49

Expert Comment

ID: 39928499
In addition to the notes provided already about the pseudo-colun rownum, you cannot guarantee the "order of output" will be what you expect without actually stipulating an order by clause.

Instead you could use the row_number() analytic function with more precision and predicable behaviour than by rownum.
``````SELECT *
FROM (
SELECT
p.fldstring folder_level_1
, p2.fldstring folder_level_2
, p3.fldstring folder_level3
, d.doc_name
, row_number() over (order by p.fldstring, p2.fldstring, p3.fldstring) as rr /* use row_number() ?? */
FROM document d
INNER JOIN folder f ON d.id = f.lid
LEFT JOIN PATH p ON f.foldid = p.fldtype || '-' || p.fldkey
LEFT JOIN PATH p2 ON p2.fldkey = p.fldkey2
LEFT JOIN PATH p3 ON p3.fldkey = p2.fldkey2
)
WHERE rr = 2 AND rr = 3;
``````
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…
Via a live example, show how to take different types of Oracle backups using RMAN.
###### Suggested Courses
Course of the Month2 days, 12 hours left to enroll