?
Solved

Oracle: How to calculate time according to row

Posted on 2011-03-03
6
Medium Priority
?
492 Views
Last Modified: 2013-12-19
Hello Again,
I have a table that holds data regarding the users' activity on the phone. It looks like this

Agent      date      signofftime      status
John      1/4/2011      11:17:33      BREAK                        
John      1/4/2011      11:35:56      Signed ON
John      1/4/2011      13:47:08      LUNCH                        
John      1/4/2011      14:22:43      Signed ON
Jane      1/4/2011      11:18:23      BREAK                        
Jane      1/4/2011      11:34:23      Signed ON
Jane      1/4/2011      12:00:05      LUNCH                        
Jane      1/4/2011      12:30:24      Signed ON

I want to calculate the time difference in hh:mm:ss between break and lunch status and the 'SignOn' time that immediately follows on the next row.

This table holds multiple users and multiple dates. It is certain tho that the Break and Lunch statuses are always followed by Signed on.

thank you in advance!

lemmohr
0
Comment
Question by:lemmohr
[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
6 Comments
 

Author Comment

by:lemmohr
ID: 35027031
how do i edit the points here? I put 250 by mistake, should be 500
0
 
LVL 74

Accepted Solution

by:
sdstuber earned 2000 total points
ID: 35027061
SELECT *
  FROM (SELECT agent,
               status,
               t,
               TO_CHAR(
                   TRUNC(SYSDATE)
                   + (t - LAG(t) OVER (PARTITION BY agent ORDER BY t)),
                   'hh24:mi:ss'
               )
                   diff
          FROM (SELECT agent,
                       TO_DATE(
                           signoffdate || signofftime,
                           'mm/dd/yyyyhh24:mi:ss'
                       )
                           t,
                       status
                  FROM yourtable))
 WHERE diff IS NOT NULL
ORDER BY agent, t
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 35027070
I'm assuming your date strings are  mm/dd/yyyy,  if not, adjust the format accordingly in the to_date call
0
Independent Software Vendors: 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 32

Expert Comment

by:awking00
ID: 35028839
Given your example, what do you expect the output to look like?
0
 

Expert Comment

by:NewDan526
ID: 35037756
Accurate analysis will save a lot of (re)development time and cost.  So its worth commenting on here...

ASSUMPTIONS/COMMENTS:
1. Your table looks like this: (I would consider making date and signofftime one timestamp column - unless you have a specific reason not to)
      CREATE TABLE timeclock
      (
         agent        VARCHAR2(20),
         signofftime  TIMESTAMP,
         status       VARCHAR2(20)
      );

2.  Your objective is to calculate the time your (presumably hard-working) employees are not working.

3. You are not addressing the last time on one day to the first on the next, and there are no night shifts crossing midnight.

4.  Your are NOT calculating how long they work (which would be a useful metric for the business since you should know when they leave for the day)

5. You are NOT covering any variations or exceptions (at this point), everybody always clocks in and out appropriately, and there are no data errors.

-------------
SOLUTION:
SELECT *
  FROM (SELECT agent,
               LAG(status) OVER (PARTITION BY agent ORDER BY SIGNOFFTIME) status,
               (SIGNOFFTIME - LAG(SIGNOFFTIME)
                  OVER (PARTITION BY agent ORDER BY SIGNOFFTIME)) as diff,
               SIGNOFFTIME
          FROM TIMECLOCK
          )
 WHERE diff IS NOT NULL
   and status in('BREAK','LUNCH')
ORDER BY agent, SIGNOFFTIME

--------------
RESULTS:
"AGENT"      "STATUS"      "DIFF"      "SIGNOFFTIME"
"Jane"      "BREAK"      0:16:0.0      04-JAN-11 11.34.23.000000000 AM
"Jane"      "LUNCH"      0:30:19.0      04-JAN-11 12.30.24.000000000 PM
"John"      "BREAK"      0:18:23.0      04-JAN-11 11.35.56.000000000 AM
"John"      "LUNCH"      0:35:35.0      04-JAN-11 02.22.43.000000000 PM
0
 

Author Closing Comment

by:lemmohr
ID: 35189721
i apologize, I thought i already awarded the points.
0

Featured Post

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!

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 2) Part 1: http://www.e-e.com/A_8429.html Previously, I introduced the basics of network ACL's including how to create, delete and modify entries to allow and deny access.  For many…
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 setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines
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…
Suggested Courses

752 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