Solved

MaxDate and Second to max date

Posted on 2008-10-17
3
1,790 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Problem with duplicate records in Oracle query 16 51
Alternative to GTT for a temp table for further reuse in Oracle 8 44
Procedure syntax 5 48
error doing substr 3 33
This article started out as an Experts-Exchange question, which then grew into a quick tip to go along with an IOUG presentation for the Collaborate confernce and then later grew again into a full blown article with expanded functionality and legacy…
Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
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.
Via a live example, show how to take different types of Oracle backups using RMAN.

713 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