Solved

Last record chosen in Oracle Query

Posted on 2016-10-19
3
34 Views
Last Modified: 2016-10-20
I need to only retrieve the last record when this Oracle 9.2 query executes.

DI  2016-09-26  168398    063848979   //SRC RMS


I would also like another Oracle query that only retrieves the first record.

NULL      DI  2016-09-16  174261    063848979   //SRC RMS             FB BRAMPTON TO

Right now,  the query will retrieve 3 records for this problem when it executes.
The 1993237 will be a field once I get it to work.
This Oracle query executes inside a SQL query.




SELECT (CASE WHEN TO_CHAR(aa.NOTES)  IS NOT NULL  THEN  TO_CHAR(aa.NOTES)  ELSE TO_CHAR(a.PTEXT) END) as MyNote
FROM            GPCOMP1.GPPROB a, GPCOMP1.GPPROBNOTES aa
WHERE        a.PROBLEM_ID = aa.PROB_ID AND (a.PROBLEM_ID = 1993237)
ORDER BY a.MODIFIED_ON DESC




 Oracle Query
0
Comment
Question by:thayduck
  • 2
3 Comments
 
LVL 76

Accepted Solution

by:
slightwv (䄆 Netminder) earned 500 total points
ID: 41850949
First row:
select mynote from (
SELECT (CASE WHEN TO_CHAR(aa.NOTES)  IS NOT NULL  THEN  TO_CHAR(aa.NOTES)  ELSE TO_CHAR(a.PTEXT) END) as MyNote,
row_number() over(order by modified_on asc) rn
 FROM            GPCOMP1.GPPROB a, GPCOMP1.GPPROBNOTES aa
 WHERE        a.PROBLEM_ID = aa.PROB_ID AND (a.PROBLEM_ID = 1993237)
)
where rn=1


Last row:
select mynote from (
SELECT (CASE WHEN TO_CHAR(aa.NOTES)  IS NOT NULL  THEN  TO_CHAR(aa.NOTES)  ELSE TO_CHAR(a.PTEXT) END) as MyNote,
row_number() over(order by modified_on desc) rn
 FROM            GPCOMP1.GPPROB a, GPCOMP1.GPPROBNOTES aa
 WHERE        a.PROBLEM_ID = aa.PROB_ID AND (a.PROBLEM_ID = 1993237)
)
where rn=1


I'm guessing that really isn't what you want.  Can you provide some sample data and exepected results?
0
 

Author Comment

by:thayduck
ID: 41852218
Your code did exactly what I wanted.

 I did have to change :

 row_number() over(order by modified_on desc) rn

 TO

 row_number() over(order by a.modified_on desc) rn


 Thank you for the quick response.
0
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
ID: 41852228
No problem.  Glad to help.
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

Suggested Solutions

Article by: Swadhin
From the Oracle SQL Reference (http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/queries006.htm) we are told that a join is a query that combines rows from two or more tables, views, or materialized views. This article provides a glimps…
Subquery in Oracle: Sub queries are one of advance queries in oracle. Types of advance queries: •      Sub Queries •      Hierarchical Queries •      Set Operators Sub queries are know as the query called from another query or another subquery. It can …
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…

708 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

13 Experts available now in Live!

Get 1:1 Help Now