Solved

Selecting the first record in a subquery in Oracle

Posted on 2014-04-10
2
1,669 Views
Last Modified: 2014-04-10
Experts,

I want to read a table that has multiple records with the same ID number to get a value.  Is there a way in Oracle to select the first record only that meets the criteria?

I would like to use a subquery and I don't want to get the error multiple rows returned.
0
Comment
Question by:morinia
2 Comments
 
LVL 16

Assisted Solution

by:Wasim Akram Shaik
Wasim Akram Shaik earned 250 total points
Comment Utility
I think you are talking to select distinct records from a table which has duplicate values

select * FROM your_table
WHERE rowid not in
(SELECT MIN(rowid)
FROM your_table
GROUP BY column1, column2, column3)

where column1,column2,column3 are the duplicate columns, this will list you non-duplicate rows
0
 
LVL 15

Accepted Solution

by:
Devinder Singh Virdi earned 250 total points
Comment Utility
Another method

select * from
(
  select * from table where some_condition=true
) where rownum < 2;

>>I would like to use a subquery and I don't want to get the error multiple rows returned.

I am assuming you are trying to use this as inline view.
ex.
select a, b, (select statement) X from table etc
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Have you ever had to make fundamental changes to a table in Oracle, but haven't been able to get any downtime?  I'm talking things like: * Dropping columns * Shrinking allocated space * Removing chained blocks and restoring the PCTFREE * Re-or…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Via a live example, show how to take different types of Oracle backups using RMAN.
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.

744 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

9 Experts available now in Live!

Get 1:1 Help Now