Solved

Difference between dates stored in Same column

Posted on 2009-05-10
7
1,059 Views
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)

Thanks
0
Comment
Question by:gvsbnarayana
  • 4
  • 3
7 Comments
 
LVL 25

Expert Comment

by:lwadwell
ID: 24351337
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 ...

lwadwell
0
 
LVL 8

Author Comment

by:gvsbnarayana
ID: 24351366
My partition column is the status_id as per the above example I provided. I tried using the lag but no luck.
0
 
LVL 25

Expert Comment

by:lwadwell
ID: 24351373
lwadwell,

please show me your SQL.

lwadwell
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 8

Author Comment

by:gvsbnarayana
ID: 24351410
SELECT   (SELECT user_status
            FROM apps.per_assignment_status_types
           WHERE assignment_status_type_id =
                                           a.assignment_status_type_id)
                                                                       status,
         (a.status_change_date - b.status_change_date) AS days,        
         a.arowno,
         b.browno
    FROM (SELECT assignment_status_id, status_change_date, assignment_id,
                 assignment_status_type_id,
                 ROW_NUMBER () OVER (PARTITION BY assignment_status_type_id ORDER BY status_change_date)
                                                                       arowno
            FROM apps.irc_assignment_statuses) a,
         (SELECT assignment_status_id, status_change_date, assignment_id,
                 assignment_status_type_id,
                 ROW_NUMBER () OVER (PARTITION BY assignment_status_type_id ORDER BY status_change_date)
                                                                       browno
            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
0
 
LVL 25

Accepted Solution

by:
lwadwell earned 125 total points
ID: 24351475
gvsbnarayana,

Try this simple SQL first ...

BTW.  Are you sure the partitioning key is assignment_status_type_id?  These values must be the same across the multiple rows to form a partion.  Your examples in the original question had the values as different for each row.

lwadwell

SELECT assignment_status_id, 
       assignment_id,
       assignment_status_type_id,
       status_change_date, 
       LAG (status_change_date,1,null) OVER (PARTITION BY assignment_id ORDER BY status_change_date) prev_date
  FROM apps.irc_assignment_statuses
 WHERE assignment_id = 649650
 ORDER BY assignment_status_id, 
       status_change_date

Open in new window

0
 
LVL 8

Author Comment

by:gvsbnarayana
ID: 24351527
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.

Awesome...
Thanks for the idea.  
0
 
LVL 8

Author Closing Comment

by:gvsbnarayana
ID: 31580018
Thanks for showing me the right direction
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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
Oracle Listener Not Starting 11 63
Creation date for a PDB 5 60
how to double quote a string for an inline sql statement. 8 88
Fill Null values 5 36
Why doesn't the Oracle optimizer use my index? Querying too much data Most Oracle developers know that an index is useful when you can use it to restrict your result set to a small number of the total rows in a table. So, the obvious side…
This post first appeared at Oracleinaction  (http://oracleinaction.com/undo-and-redo-in-oracle/)by Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

679 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