Solved

Oracle: How to calculate time according to row

Posted on 2011-03-03
6
486 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
6 Comments
 

Author Comment

by:lemmohr
Comment Utility
how do i edit the points here? I put 250 by mistake, should be 500
0
 
LVL 73

Accepted Solution

by:
sdstuber earned 500 total points
Comment Utility
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 73

Expert Comment

by:sdstuber
Comment Utility
I'm assuming your date strings are  mm/dd/yyyy,  if not, adjust the format accordingly in the to_date call
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 31

Expert Comment

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

Expert Comment

by:NewDan526
Comment Utility
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
Comment Utility
i apologize, I thought i already awarded the points.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

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…
Have you ever had to make fundamental changes to a table in Oracle, but haven't been able to get any downtime?  I'm talking things like: * Dropping columns * Shrinking allocated space * Removing chained blocks and restoring the PCTFREE * Re-or…
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now