Solved

take min value from the table.

Posted on 2014-02-05
5
405 Views
Last Modified: 2014-02-16
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
Comment
Question by:sakthikumar
5 Comments
 
LVL 24

Accepted Solution

by:
chaau earned 250 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;

Open in new window

0
 
LVL 37

Assisted Solution

by:Geert Gruwez
Geert Gruwez earned 250 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

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

Expert Comment

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

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

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
Oracle RAC 12c 8 88
Creation date for a PDB 5 39
update statement in oracle 9 30
join actual table rows based on the column 25 22
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…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

820 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