Solved

PL/SQL query help

Posted on 2014-04-10
6
249 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 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 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Why doesn't the Oracle optimizer use my index? Querying too much data Most Oracle developers know that an index is useful when you can use it to restrict your result set to a small number of the total rows in a table. So, the obvious side…
Note: this article covers simple compression. Oracle introduced in version 11g release 2 a new feature called Advanced Compression which is not covered here. General principle of Oracle compression Oracle compression is a way of reducing the d…
Via a live example, show how to take different types of Oracle backups using RMAN.
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

696 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