Solved

PL/SQL query help

Posted on 2014-04-10
6
250 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
[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
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 35

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 35

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 49

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
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 information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

717 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