Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

MaxDate and Second to max date

Posted on 2008-10-17
3
Medium Priority
?
1,795 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 500 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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

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…
Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.
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

885 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