Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

PL/SQL query help

Posted on 2014-04-10
6
Medium Priority
?
253 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 2000 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…

618 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