Solved

rownum inbetween

Posted on 2014-03-13
4
319 Views
Last Modified: 2014-03-13
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;

Open in new window

Please assist
0
Comment
Question by:CalmSoul
4 Comments
 
LVL 142

Accepted Solution

by:
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

by:awking00
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

by:Naveen Kumar
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 48

Expert Comment

by:PortletPaul
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;

Open in new window

0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL query bug 3 28
Get the parent node - XMLTYPE 9 56
email about the whoisactive result 7 21
Help Parsing a String with SQL Syntax 23 25
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now