Solved

First result only from a subquery.

Posted on 2006-11-09
7
1,159 Views
Last Modified: 2007-11-27
Hi all,

Here's an MS-SQL subquery I need to duplicate under Oracle:

(SELECT TOP 1 AField FROM ATable WHERE (OtherTable.A = A) AND (OtherTable.B = B) AND (OtherTable.C = C)) AS Result

Am I right in assuming that this will be equivalent:

(SELECT AField FROM ATable WHERE ( ROWNUM = 1 ) AND (OtherTable.A = A) AND (OtherTable.B = B) AND (OtherTable.C = C)) AS Result

or should it be:

(SELECT AField FROM Table WHERE (OtherTable.A = A) AND (OtherTable.B = B) AND (OtherTable.C = C) AND (ROWNUM = 1)) AS Result

or is there no difference or will this not work? If the latter, how can I make it work?

A speedy response would be greatly appreciated. :-)

Paul
0
Comment
Question by:PaulCaswell
7 Comments
 
LVL 22

Expert Comment

by:Ivo Stoykov
ID: 17905973
Hi PaulCaswell,

in general there is equal how you will write where clause. Oracle optimiser will choose the best way.
inner brackets are not necessary because you're using AND

a potential problem is that there is no join (if necessary at all) between Table and OtherTable...

Regards

!i!
0
 
LVL 14

Expert Comment

by:GGuzdziol
ID: 17906019
MSSQL - SELECT TOP 1 ...
Oracle - SELECT * FROM (SELECT ... ) WHERE ROWNUM = 1

If Your query doesn't have GROUP BY / ORDER BY clause You can make ROWNUM = 1 part of Your original query - otherwise You have to put it outside.
0
 
LVL 14

Accepted Solution

by:
GGuzdziol earned 500 total points
ID: 17906064
Btw. order of predicates doesn't matter, so both of queries are identical. All others (without rownum) will be evaluated, rows will be numbered and then ROWNUM = 1 applied.
0
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.

 
LVL 34

Expert Comment

by:Mark Geerlings
ID: 17906084
I don't know MS-SQL, so I don't completely understand the MS-SQL syntax.  In pseudo-code, is this what you are trying to do:
select the highest value from fieldA in tableA
where tableA, fieldA = some valueA
and tableA, fieldB = some valueB
and tableA, fieldC = some valueC ?

I also want to give you a couple cautions about Oracle.  First, Oracle has no "top" command.  Oracle does support "max" (and "min" and other group operators).  Second, Oracle does have "rownum", but you have to be *VERY* careful with using that, since it is applied as rows are retrieved (which is often, but not always in the order they were inserted) but *BEFORE* the rows are sorted based on an "order by" clause, or a "max" or other group operator.  It is possible in Oracle to use a nested select (a type of sub-query which includes an "order by", or a group operator like: "max") then use rownum in the outer select to retrieve the first (top) row from the nested select.
0
 
LVL 16

Author Comment

by:PaulCaswell
ID: 17906192
Thanks people.

Just to confirm: ATable and OtherTable share columns A, B and C. My main query is on OtherTable. I need to find the record in ATable that has the same values of A, B, and C as in OtherTable and pull the AField from it but occasionally (very rarely) there can be two such records in ATable. Obviously this will cause an error so I must avoid it.

I need to be confident here as this situation might only happen a few times a year and this query may be executed many times per day.

>>Btw. order of predicates doesn't matter, so both of queries are identical. All others (without rownum) will be evaluated, rows will be numbered and then ROWNUM = 1 applied.
I think this answers my question. Am I right?

Paul
0
 
LVL 14

Expert Comment

by:GGuzdziol
ID: 17906396
If Your main concern was if this matters (I mean - if this is evaluated in the moment You expect it to be, after other conditions) - then yes.

General truth about migrating TOP N queries from SQL Server to Oracle is in my first post. Those examples are semantically equivalent.
0
 
LVL 16

Author Comment

by:PaulCaswell
ID: 17908536
Thanks! It worked! :-)
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.

Join & Write a Comment

Suggested Solutions

Working with Network Access Control Lists in Oracle 11g (part 1) Part 2: http://www.e-e.com/A_9074.html So, you upgraded to a shiny new 11g database and all of a sudden every program that used UTL_MAIL, UTL_SMTP, UTL_TCP, UTL_HTTP or any oth…
Introduction A previously published article on Experts Exchange ("Joins in Oracle", http://www.experts-exchange.com/Database/Oracle/A_8249-Joins-in-Oracle.html) makes a statement about "Oracle proprietary" joins and mixes the join syntax with gen…
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

760 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

21 Experts available now in Live!

Get 1:1 Help Now