Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

MaxDate and Second to max date

Posted on 2008-10-17
3
Medium Priority
?
1,793 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Working with Network Access Control Lists in Oracle 11g (part 1) Part 2: http://www.e-e.com/A_9074.html So, you upgraded to a shiny new 11g database and all of a sudden every program that used UTL_MAIL, UTL_SMTP, UTL_TCP, UTL_HTTP or any oth…
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…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

719 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