We help IT Professionals succeed at work.

Difference between dates stored in Same column

gvsbnarayana asked
Last Modified: 2013-12-19
Hi Experts
I am actually stuck with a logic problem on a SQL query in Oracle.  
I need the difference between two dates (that are in date datatypes) that are in same column.
I tried using the ROW_NUMBER () OVER (PARTITION BY) concept but its yielding incorrect values at some places. The "Days in each status" output column is what I am looking for.  The status id is the column which has few set of statuses which get repeated for each unique person.

STATUS_CHANGE_DATE      status_id      Days in each status
4/9/2009 6:51      220      0 (there is no value above it so it should be 0)
4/6/2009 7:57      114      4 (diff between the 4/9 and 4/6 incl both the days)
4/6/2009 7:56      6      1 (diff between 4/6 =0 but should consider it as1)
4/6/2009 7:56      5      1
3/30/2009 14:30      111      8 (diff between 4/6 and t3/30)
3/26/2009 9:04      208      5 (diff between 3/30 and 3/26)
3/25/2009 16:17      49      2 (diff between 3/26 and 3/25)

Watch Question

Lee WadwellProject Architect

Hi gvsbnarayana,

The LAG() function would be better than the ROW_NUMBER() for this I think.  What is your PARTITION BY column(s)?  The LAG() function allows you to look back 'n' rows (the other one is LEAD() which allows you to look forward 'n' rows).  You could do something like

SELECT status_change_date, LAG(status_change_date,1,null) OVER(partition by some_column ORDER BY status_change_date) as prev_date
FROM ...



My partition column is the status_id as per the above example I provided. I tried using the lag but no luck.
Lee WadwellProject Architect


please show me your SQL.



SELECT   (SELECT user_status
            FROM apps.per_assignment_status_types
           WHERE assignment_status_type_id =
         (a.status_change_date - b.status_change_date) AS days,        
    FROM (SELECT assignment_status_id, status_change_date, assignment_id,
                 ROW_NUMBER () OVER (PARTITION BY assignment_status_type_id ORDER BY status_change_date)
            FROM apps.irc_assignment_statuses) a,
         (SELECT assignment_status_id, status_change_date, assignment_id,
                 ROW_NUMBER () OVER (PARTITION BY assignment_status_type_id ORDER BY status_change_date)
            FROM apps.irc_assignment_statuses) b
   WHERE a.assignment_status_type_id = b.assignment_status_type_id
     AND a.arowno - 1 = b.browno
     AND b.assignment_id = 649650
     order by a.status_change_date
Project Architect
This one is on us!
(Get your first solution completely free - no credit card required)


The Assignment Status Type id gets repeated to each Assignment id. The Assignment Status table holds the lookup values for the statuses table. So, per defintion, to form a partition I considered the  assignment status type_id as the partition key. I think that was the mistake I did.

But as per the above query I got the values for the prev value as you considered the assignment id itself and I made my modifications accordingly.

Thanks for the idea.  


Thanks for showing me the right direction

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.