Solved

Last record chosen in Oracle Query

Posted on 2016-10-19
3
62 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.

Question has a verified solution.

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

Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…
How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
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.

810 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