Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8779985
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:55:54+00:00 2026-06-13T19:55:54+00:00

I have a query I run to tell me the latest note for active

  • 0

I have a query I run to tell me the latest note for active participants:

select notes.applicant_id,
   reg.program_code,
   reg.last_name,
   reg.first_name,
   reg.status_cd,
   MAX(notes.service_date) as "Last Note"
from reg inner join notes on reg.applicant_id=notes.applicant_id
where reg.status_cd='AC'
group by notes.applicant_id, reg.program_code, 
         reg.last_name, reg.first_name, reg.reg_date, 
         reg.region_code, reg.status_cd
order by MAX(notes.service_date)

But I would also like this query to give me the result of the note.service_date just prior to the max service_date as well.

Results would look like this

notes.applicant_id   reg.last_name  reg.first_name reg.status_cd  Last Note    Prior Note
 12345                 Johnson          Lori           AC        01-NOV-2011   01-OCT-2011

I am working in oracle.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T19:55:56+00:00Added an answer on June 13, 2026 at 7:55 pm

    You can use the lag function, or join it with the same table.

    Here is a simpler example (you haven’t givven us data sample):

    create table t as
    (select level as id, mod(level , 3) grp, sysdate - level dt
    from dual 
    connect by level < 100
    )
    

    and here are the queries:

    select t2.grp,t1.grp, max(t1.dt) mdt, max(t2.dt) pdt
      from t t1
      join t t2 on t1.dt < t2.dt and t1.grp = t2.grp
     group by t2.grp, t1.grp;
    

    or

    select grp, max(pdt), max(dt)
     from(
     select grp, lag(dt) over (partition by grp order by dt) pdt, dt 
     from t)
     group by grp
    

    Here is a fiddle


    In your case it could be something like this:

    select t.applicant_id, t.program_code, 
             t.last_name, t.first_name, t.reg_date, 
             t.region_code, t.status_cd,
             max(t.dt) as "Last Note",
             max(t.pdt) as "Prev Note"
    from (
    select notes.applicant_id,
       reg.program_code,
       reg.last_name,
       reg.first_name,
       reg.status_cd,
       notes.service_date as dt,
       lag(notes.service_date)  over (partition by notes.applicant_id,
       reg.program_code,
       reg.last_name,
       reg.first_name,
       reg.status_cd order by notes.service_date) as pdt
    from reg inner join notes on reg.applicant_id=notes.applicant_id
    where reg.status_cd='AC'
    ) t
    group by t.applicant_id, t.program_code, 
             t.last_name, t.first_name, t.reg_date, 
             t.region_code, t.status_cd
    order by MAX(t.dt)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using in C# MYsql .I have query that works if I run
I have the next query that I run via SQLCMD.EXE use [AxDWH_Central_Reporting] GO EXEC
I have a basic query that I run over and over at each 2
I have the query below that when run it says that 325 rows were
I have a query which works fine in MySQL, but when I run it
My scenario is this: I have a base NHibernate query to run of the
As I had written in title, I have SQL query, run on Oracle DB,
I have code to run sql query in ruby as follows sql = ActiveRecord::Base.connection()
I have a custom SQL query that I run with this line: @avg_score =
i have using aqua data studio to run my query from sql server 2005.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.