Solved

PL/SQL query help

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

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Get FileTypes from a list of FileNames  in a Table field 18 49
passing parameters to sql script oracle 4 41
SQL Developer 6 48
constraint check 2 40
Truncate is a DDL Command where as Delete is a DML Command. Both will delete data from table, but what is the difference between these below statements truncate table <table_name> ?? delete from <table_name> ?? The first command cannot be …
Have you ever had to make fundamental changes to a table in Oracle, but haven't been able to get any downtime?  I'm talking things like: * Dropping columns * Shrinking allocated space * Removing chained blocks and restoring the PCTFREE * Re-or…
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.
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…

776 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