If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Date difference between rows of records

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-27-10, 00:25
ngaisteve ngaisteve is offline
Registered User
 
Join Date: Jul 2010
Posts: 4
Date difference between rows of records

ID | STATUS | DATE | SYSTEMTIME
--------------------------------------------------------------------
ABC | S | 2010-01-14 | 2010-01-15-00.03.07.205829
ABC | E | 2010-01-14 | 2010-01-15-00.16.16.473507
ABC | S | 2010-01-15 | 2010-01-15-22.52.54.072351
ABC | E | 2010-01-15 | 2010-01-15-23.04.34.257471

Hi, I would like to ask, is it possible for SQL in DB2 to calculate the above test data to something like below?

PROGID | DATE | Duration (hh:mm:ss)
-------------------------------------------------------
ABC | 2010-01-14 | 00:13:09
ABC | 2010-01-15 | 00:11:40

Note: S - Start; E - End.

Last edited by ngaisteve; 07-27-10 at 00:29.
Reply With Quote
  #2 (permalink)  
Old 07-27-10, 01:42
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,198
Yes, it is possible.
__________________
M. A. Feldman
IBM Certified DBA on DB2 for Linux, UNIX, and Windows
IBM Certified DBA on DB2 for z/OS and OS/390
Reply With Quote
  #3 (permalink)  
Old 07-27-10, 08:02
dav1mo dav1mo is offline
Registered User
 
Join Date: Dec 2007
Location: Richmond, VA
Posts: 782
you would have to join the table to itself in a subquery getting the max SYSTEMTIME of the E status that is less than the SYSTEMTIME of your S status.
Dave
Reply With Quote
  #4 (permalink)  
Old 07-27-10, 08:03
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
You can do a self-join and then calculate the difference:
Code:
SELECT t1.progid, t1.date, t2.systemtime - t1.systemtime
FROM   ... AS t1 JOIN ... AS t2 ON ( t1.progid = t2.progid AND t1.date = t2.date )
WHERE  t1.status = 'S' AND
       t2.status = 'E'
The basic idea is to get the start and end timestamps into a single row so that you can process that row in the SELECT list and/or WHERE clause.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #5 (permalink)  
Old 07-27-10, 08:55
tonkuma tonkuma is online now
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,195
Another way may be like this:

Code:
SELECT id AS progid , date
     , /* calculate a difference of
            MAX(CASE status WHEN 'E' THEN systemtime END)
            and
            MAX(CASE status WHEN 'S' THEN systemtime END)
          then convert it to data type TIME.
       */ AS duration
  FROM test_data
 GROUP BY
       id , date
;
Reply With Quote
  #6 (permalink)  
Old 07-28-10, 06:40
ngaisteve ngaisteve is offline
Registered User
 
Join Date: Jul 2010
Posts: 4
okay, thanks a lot. I will try them out.
Reply With Quote
  #7 (permalink)  
Old 07-28-10, 22:22
ngaisteve ngaisteve is offline
Registered User
 
Join Date: Jul 2010
Posts: 4
Quote:
Originally Posted by stolze View Post
You can do a self-join and then calculate the difference:
Code:
SELECT t1.progid, t1.date, t2.systemtime - t1.systemtime
FROM   ... AS t1 JOIN ... AS t2 ON ( t1.progid = t2.progid AND t1.date = t2.date )
WHERE  t1.status = 'S' AND
       t2.status = 'E'
The basic idea is to get the start and end timestamps into a single row so that you can process that row in the SELECT list and/or WHERE clause.
It works. Thanks a lot.
Reply With Quote
  #8 (permalink)  
Old 07-28-10, 22:49
ngaisteve ngaisteve is offline
Registered User
 
Join Date: Jul 2010
Posts: 4
To be more accurate, change t2.systemtime - t1.systemtime to
timestampdiff(2, char(timestamp(to_char(timestamp(t2.systemtime), 'yyyy-mm-dd hh24:mi:ss')) - timestamp(to_char(timestamp(t1.systemtime), 'yyyy-mm-dd hh24:mi:ss')) ))
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On