Solved

PL/SQL query help

Posted on 2014-04-10
6
240 Views
Last Modified: 2014-05-06
I need to write a SQL as:

select max(Col1), Col3, Col4
from Tbl1
where Col2=5

I then need the corresponding Col3 and Col4 values for the max(Col1) record. I tried several combinations of PARTITION/OVER BY but it didn't work. I was able to resolve it using two SQLs. I would like to do it in one SQL instead of doing it in two and doing a join between the PKs.  Thanks.
0
Comment
Question by:soccerplayer
  • 3
  • 2
6 Comments
 
LVL 34

Accepted Solution

by:
johnsone earned 500 total points
ID: 39991903
I think this is what you are looking for:

SELECT col1, 
       col3, 
       col4 
FROM   (SELECT Row_number() 
                 over ( 
                   ORDER BY col1 DESC) rn, 
               col1, 
               col3, 
               col4 
        FROM   tbl1 
        WHERE  col2 = 5) 
WHERE  rn = 1 

Open in new window


I believe if you had a partition by clause it would be on col2, and since you are only selecting one value for col2 it shouldn't be needed.
0
 

Author Comment

by:soccerplayer
ID: 39991958
Your SQl does not give the right values. Remember that there is a max here that needs to be done.
0
 

Author Comment

by:soccerplayer
ID: 39992006
Some sample values in Tbl1:

Col1 Col2 Col3 Col4
1      A      P      X
3      B      Q     Z
2      C      R      Z

So I want the 2nd row.
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:johnsone
ID: 39992325
Your sample data makes no sense.  According to your specification, col2 should be a number and you should be looking for rows with a 5 in that column.

Here is the query I ran:

SELECT col1, 
       col2, 
       col3, 
       col4 
FROM   (SELECT Row_number() 
                 over ( 
                   ORDER BY col1 DESC) rn, 
               col1, 
               col2, 
               col3, 
               col4 
        FROM   tbl1) a 
WHERE  rn = 1; 

Open in new window


The only modification from the original is that I added col2 and removed the where clause for col2=5 because that doesn't make sense given your sample data.

That query comes out with the row that you asked for.  There is no need for the MAX function, the order by and row number functions handle that.

If there is an issue, please provide sample data that shows the issue.  The sample data you provided gives the correct result.
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 39993256
You could also try this approach:

SELECT
      col3
    , col4
FROM table1
WHERE col1 = (SELECT MAX(col1) FROM table1)
;

result
| COL3 | COL4 |
|------|------|
|    Q |    Z |

Open in new window

NOTE IF col1=3 exists for more than one record then each of those will be listed,

You could arbitrarily limit to one record using rownum if only one record is required, but you don't specify what should happen for this circumstance (and if this circumstance exists which record is the right record?)
0
 

Author Closing Comment

by:soccerplayer
ID: 40045443
Thanks. Might re-visit this later. Don't have time to verify now.
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

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.
From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
Via a live example, show how to take different types of Oracle backups using RMAN.
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

758 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

16 Experts available now in Live!

Get 1:1 Help Now