Solved

MaxDate and Second to max date

Posted on 2008-10-17
3
1,779 Views
Last Modified: 2013-12-07
Hi all,

Ok Ill provide 2 records to start the ball rolling

Worksrollnumber               Surname                 Latest Pay Date    Amount      Last Pay Date     Amount
   123456                             Bloggs                     21/10//08              890              21/09/08            890
   789101                             Doe                          21/010/08            1000             21/02/08           650

Ok, the 2 records above are what I want to see, and a comparison will be done to highlight differences in monthly pension payments.   What I need to do is find the Max Date of all employees i.e their Latest pay Date and then the Second to max date, which means when a last entry was made, so it is 21/09/08 for Bloggs and 21/02/08 for Doe.  I have written a query to get the Max Date for each row.  Now I need to find the second to max date.  I have tried multiple nested queries and also created a MaxDate view and queried that but I only got 'too many values'  What I am trying to do is get a maxdate of the set of dates which arent equal the current max date.   Sounds simple but it has been a ball ache :/  

Thanks
0
Comment
Question by:BPMonk
3 Comments
 
LVL 27

Accepted Solution

by:
sujith80 earned 125 total points
ID: 22739082
You can use a query like this:

select surname, max(case when cn = 1 then pay_date else null end) latest_pay,
max(case when cn = 2 then pay_date else null end) last_pay
from (
select surname, pay_date, row_number() over(partition by surname order by pay_date desc) cn
from <your table>
)
group by surname;
SQL> select * from tbl1;
 

SURNAME              PAY_DATE

-------------------- --------

Bloggs               21/10/08

Bloggs               21/09/08

Bloggs               21/08/08

Doe                  21/10/08

Doe                  21/02/08

Doe                  21/01/08
 

6 rows selected.
 

SQL> 

SQL> select surname, max(case when cn = 1 then pay_date else null end) latest_pay,

  2  max(case when cn = 2 then pay_date else null end) last_pay

  3  from (

  4  select surname, pay_date, row_number() over(partition by surname order by pay_date desc) cn 

  5  from tbl1

  6  )

  7  group by surname;
 

SURNAME              LATEST_P LAST_PAY

-------------------- -------- --------

Bloggs               21/10/08 21/09/08

Doe                  21/10/08 21/02/08
 

SQL> 

Open in new window

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

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.  …
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.

757 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

18 Experts available now in Live!

Get 1:1 Help Now