Solved

MaxDate and Second to max date

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Error executing command from server 6 49
Clone Oracle 12c Database 5 52
Oracle SQL - Query help 7 54
how to trim oracle sql sentence in unix 17 52
Introduction A previously published article on Experts Exchange ("Joins in Oracle", http://www.experts-exchange.com/Database/Oracle/A_8249-Joins-in-Oracle.html) makes a statement about "Oracle proprietary" joins and mixes the join syntax with gen…
How to Create User-Defined Aggregates in Oracle Before we begin creating these things, what are user-defined aggregates?  They are a feature introduced in Oracle 9i that allows a developer to create his or her own functions like "SUM", "AVG", and…
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

776 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