Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# take min value from the table.

Posted on 2014-02-05
Medium Priority
410 Views
for eg. I have a table with name EXAMPLE with four columns col1,col2,col3,col4

I have a query like below

select col1,col2,col3,col4 from example
where (condition 1)
and   (condition 2)
and (condition 3)

After executing the query I am having the below results.

col1               col2      col3     col4
------              -------     -------    -------
23                    34          56       89
45                    56          65       85
11                    32          11       63

In this result,
I need only one row, that is take the minimum value for col2 from the results and get the corresponding col1 value .

What is the simplest way to achieve this.?
0
Question by:sakthikumar
[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 25

Accepted Solution

chaau earned 1000 total points
ID: 39837876
I think this is the simplest one:
``````SELECT *
FROM (SELECT col1,col2,col3,col4 from example
where (condition 1)
and   (condition 2)
and (condition 3)
ORDER BY col2)
WHERE ROWNUM = 1;
``````
0

LVL 38

Assisted Solution

Geert Gruwez earned 1000 total points
ID: 39838105
with x as (
select col1,col2,col3,col4 from example
where (condition 1)
and   (condition 2)
and (condition 3))
select * from x
where col2 = (select min(col2) from x);
0

LVL 8

Expert Comment

ID: 39838197
An addition the comments above: there is a difference if col2 is not unique.
- chaau will return exactly one row where col2 is minimum. Which one of several records with same col2 value is undefined.
- Geert will return all the rows where col2 is minimum

Performance-wise, I feel Geert's solution faster (should matter only if we are talking about millions of rows) but if you need exactly one row you can still add the rownum clause. My habit is to use "rownum<2" instead of "rownum=1" but I don't think there's a difference in Oracle planning.
0

LVL 38

Expert Comment

ID: 39838743
> Performance-wise, I feel Geert's solution faster

it might even be faster if you add the /*+ MATERIALIZE */ hint
depends on size ... again

with x as (
select /*+ MATERIALIZE */ col1,col2,col3,col4 from example
where (condition 1)
and   (condition 2)
and (condition 3))
select * from x
where col2 = (select min(col2) from x);
0

LVL 32

Expert Comment

ID: 39839454
>>I need only one row, that is take the minimum value for col2 from the results and get the corresponding col1 value .<<
So from your example, you would just want to return 11 (i.e. the value of col1 where col2 is at its minimum)?
0

## Featured Post

Question has a verified solution.

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

How to Create User-Defined Aggregates in Oracle Before we begin creating these things, what are user-defined aggregates?  They are a feature introduced in Oracle 9i that allows a developer to create his or her own functions like "SUM", "AVG", and…
This post first appeared at Oracleinaction  (http://oracleinaction.com/undo-and-redo-in-oracle/)by Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
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
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.
###### Suggested Courses
Course of the Month9 days, 4 hours left to enroll