Calculation of the time differences between several lines

Hello

I have a table as below:

EVENT ID DATE
1. start 05/03/2012-13:00
1. stop the 15:00 05/04/2012
1. start 09:00 05/07/2012
1 stop on 05/09/2012 10:00
Start 2 08/06/2012-08:00
2 stop on 08/07/2012 10:00

I would like to calculate the duration (in hours ideally) between 'Start' and 'Stop' for each ID, so having a set of lines:

ID TimeSpan
---------------------
1 75
26 2

I would appreciate any idea how to solve this, ideally as a SQL solution or maybe a function.

Thank you very much!
Tom

The difference between two dates is a number, where 1 = 1 day. Multiply by 24 to get hours.

Use the LAG, as stated above, to compare the date prior to the current date.

create table T(TID, EVENT, TDATE) as select
1, 'start', TO_DATE('03/05/2012 13:00:00', 'dd/mm/yyyy hh24:mi:ss') from DUAL union all select
1, 'Stop',  TO_DATE('04/05/2012 15:00:00', 'dd/mm/yyyy hh24:mi:ss') from DUAL union all select
1, 'start', TO_DATE('07/05/2012 09:00:00', 'dd/mm/yyyy hh24:mi:ss') from DUAL union all select
1, 'Stop',  TO_DATE('09/05/2012 10:00:00', 'dd/mm/yyyy hh24:mi:ss') from DUAL union all select
2, 'start', TO_DATE('06/08/2012 08:00:00', 'dd/mm/yyyy hh24:mi:ss') from DUAL union all select
2, 'Stop',  TO_DATE('07/08/2012 10:00:00', 'dd/mm/yyyy hh24:mi:ss') from DUAL;

select tid, round(sum(hours)) hours from (
  select case
    when EVENT = 'Stop' then 24*(TDATE - LAG(TDATE) over(partition by TID order by TDATE))
    else 0 end hours,
  TID from T
)
group by tid;

TID HOURS
--- -----
  1    75
  2    26

Tags: Database

Similar Questions

  • calculate the time difference between several lines

    Hello

    I have a table as below:

    create table select TEST_CASETBL (ID, CASE_NUM, CASE_STATUS, CASE_SUB_STATUS, LAST_UPD_DTTM)

    112, 123-456', 'open', 'Work', TO_DATE (11 March 2015 13:00 ',' dd/mm/yyyy hh24:mi:ss') of the DUAL union all select

    113, 123-456', 'Open', 'pending on the admin', TO_DATE (10 January 2015 15:00 ',' dd/mm/yyyy hh24:mi:ss') of the DUAL union all select

    114, 123-456', 'Open', 'client expectation', TO_DATE (10 July 2015 09:00 ',' hh24:mi:ss' of dd/mm/yyyy) of the DUAL union all select them

    315, 123-456', 'open', 'Work', TO_DATE (September 15, 2015 10:00 ',' dd/mm/yyyy hh24:mi:ss') of the DUAL union all select

    219, 123-456', 'Open', 'pending on the admin', TO_DATE (January 9, 2015 08:00 ',' dd/mm/yyyy hh24:mi:ss') of the DUAL union all select

    651, 123-456', 'open', 'Work', TO_DATE (August 20, 2015 10:00 ',' dd/mm/yyyy hh24:mi:ss') from DUAL;

    I would like to calculate the duration total (days ideally) to CASE_SUB_STATUS, so have a set of lines:

    CASE_NUM of work waiting on admin waiting on customer

    -------------------------------------------------------------------------------------------------------------------------------------------------------

    123-456 70days 6 hours

    Here is the explanation of the pattern:

    ex: for everyday business computing (timestamp timestamp of ID:651 - ID:219) +(timestamp of ID:315-timestamp of ID:114) + (ID:112 stamp - current_time) = 11 days + 21 days 22hrs 23hrs 36 days 9 hours

    still waiting on admin

    (ID:219 timestamp - timestamp of ID:315) + (ID:113 timestamp - timestamp of ID:112)

    still waiting on the client

    (ID:114 timestamp - timestamp of ID:113)

    I would appreciate any idea how to solve this, ideally as an SQL

    Thank you

    Should he not?...

    with test_casetbl (id, case_num, case_status, case_sub_status, last_upd_dttm) as)

    Select 112, 123-456', 'Open', 'Work', TO_DATE (11 March 2015 13:00 ',' dd/mm/yyyy hh24:mi:ss') of all the DOUBLE union

    Select 113, 123-456', 'Open', 'pending on the admin', TO_DATE (10 January 2015 15:00 ',' dd/mm/yyyy hh24:mi:ss') of all the DOUBLE union

    Select 114, 123-456', 'Open', 'client expectation', TO_DATE (10 July 2015 09:00 ',' dd/mm/yyyy hh24:mi:ss') of all the DOUBLE union

    Select 315, 123-456', 'Open', 'Work', TO_DATE (September 15, 2015 10:00 ',' dd/mm/yyyy hh24:mi:ss') of all the DOUBLE union

    Select 219, 123-456', 'Open', 'pending on the admin', TO_DATE (January 9, 2015 08:00 ',' dd/mm/yyyy hh24:mi:ss') of all the DOUBLE union

    Select 651, 123-456', 'Open', 'Work', TO_DATE (August 20, 2015 10:00 ',' the hh24: mi: ss' dd/mm/yyyy) double

    ),

    t like)

    Select case_num,

    case_sub_status,

    (last_upd_dttm, 1, sysdate) ahead of diff last_upd_dttm (partition by order of last_upd_dttm case_num).

    of test_casetbl

    )

    Select case_num,

    trunc (a) | "day (s). TO_CHAR (date ' 1-1-1' + a, "fmhh24" 'mi' minute (s) "ss" second (s) hour (s)"") "work."

    trunc (b) | "day (s). TO_CHAR (day 1-1-1' + b, "fmhh24" 'mi' minute (s) "ss" second (s) hour (s)"") 'Waiting on admin',

    trunc (c) | "day (s). TO_CHAR (day 1-1-1' + c, "fmhh24" 'mi' minute (s) "ss" second (s) hour (s)"") "waiting on customer."

    t

    pivot)

    Sum (diff)

    for case_sub_status in)

    'Work' is.

    B "waiting on admin',

    C "customer expectation."

    )

    )

    CASE_NUM Working Waiting on admin Waiting on customer
    123-456 63 day (s) 22 hour (s) (s) 50 minute 49 second (s) 19 day (s) on 20 (s) hour 0 minute 0 second (s) 27 day (s) on 4 hour (s) 0 minute 0 second (s)
  • How do the time difference between two dates?

    Hi all

    I use this query to get the time difference between two dates.

    Select to_timestamp ('2012-10-03 12:00 ',' YYYY-MM-DD hh)-to_timestamp ('2012-10-03 11:00 ',' YYYY-MM-DD hh) as double diff;

    but do not get the correct result.

    Thank you

    Left KEY... Left Padding of tanks.

  • How to set the time difference between each data when using keithley 2400 scanning

    Hello friends,

    I use scanning Keithley vi the extent of SCANNING and acquire vi. I want to measure the voltage for each step and a pause between each two data, so I need a delay between each I step.

    I'm a starter to use Labview, thank you very much for your answers.

    Perry

    As Dennis says, if you use the built-in scan function, you will need to consult the manual. See Section 10-16 (this is page 10 of article 16, only paragraphs not but 10, 16) for the manual Keithley 2400.

    The Keithley 24xx series has a speed of measurement in units called PLC (Power Line Cycles). The default speed is 1PLC, which means a measure is taken with each cycle of line 1 power supply or 1/60th of a second (16.67ms). 24XX can range from 0.01 PLC (all 0.16ms) 10 PLC (all 166.6ms). The faster you measure, the less accuracy you get.

    To programmatically set this value, the command is

    ENSe:CURRent:NPLCycles

    ENSe:VOLTage:NPLCycles

    Depending on what you are sensing and where is the number of controllers from 0.01 to 10.

    Another factor that will determine the time between data points is the cycle SDM. These are more complicated, look at your Keithley manual for more information. Look at article 6 and article 11 for more information.

    Note:

    PLC times are based on a cycle of 60 Hz US.

  • Measure the time difference between a digital output and an analog input that responded to the questionnaire

    Hallo,

    I use the following system:

    • OR PXI-1044 with controller NI PXI-8109

    • OR PXI-2564 switch module to turn on the monitor of my test device

    • Data acquisition multifunction NI PXI-6259 to measure the signal that responded to the questionnaire jump

    The two cards are the same - PXI trigger bus. For both, PXI-2564 and PXI-6259 I use DAQmx to set the reading and writing of the channels.

    Now, I want to measure the time between the digital output, my unit turns and the analog input, which measures the response of my system.

    I can't do work by myself, please help me!

    I thank Ludwig.

    Hi Ludwig,.

    If you can't give us any VI we have difficulties with to help you.

    Because I Donat knowledge how your program is mounted it is not easy to know where you should enter signals.

    Here's a question similar to yours:

    http://forums.NI.com/T5/LabVIEW/best-way-to-measure-time/TD-p/178704

    and 2 external links:

    http://www.ehow.com/how_8698983_measure-time-LabVIEW.html

    http://objectmix.com/LabVIEW/385152-how-can-i-use-LabVIEW-measure-time-between-analog-pulses.html

  • There is the time difference between I phone and Apple Watch

    Ythe time on my I phone is correct, but on my Apple Watch is not

    You have sync it and paired? in the same time zone?

  • Is there an easy way to calculate the time difference?

    I am trying to find a way to calculate the time difference between

    here at a certain time

    var settime:Number = 02:35

    and

    now would be getDate();

    I tried something rediculus, but it does not work when I entered a date less than 24 hours, is there a class out there who can just this kind of things

    or y at - it something on the internet that could tell me the difference between different time

    Thank you

    My attempt

    var date: Date = new Date();

    Set timer to what would I like to call the function
    var lastcallH:Number = 22;
    var lastcallM:Number = 50;
    var lastcallS:Number = 0;

    Set up of the 24-hour clock
    var tHour:Number = 24;
    var tMinute:Number = 60;
    var tSecond:Number = 60;

    Get the current time
    var cHour:Number = date.getHours ();
    var cMinutes:Number = date.getMinutes ();
    var cSeconds:Number = date.getSeconds ();

    Set the first variable
    var fH:Number;
    var fM:Number;
    var fS: Number;

    A second set of Variable
    var sH: number;
    var sM:Number;
    var sS:Number;

    Final conversion for the Timer
    var finalMil:Number;

    Time variables that will need to be converted

    If (lastcallH < Thur)
    {
    fH = chorus - Thur;
    fM = cMinutes - tMinute;
    fS = cSeconds - tSecond.
    finalMil = (sH * 60) + (sM * 60) + (sM * 60);
    }
    ElseIf (cMinutes > 0 | cSeconds > 0) {}
    ++ Choir;
    ++ cMinutes;
    fH = chorus - Thur;
    fM = cMinutes - tMinute;
    fS = cSeconds - tSecond.
    }
    else {}
    fH = chorus - Thur;
    fM = cMinutes - tMinute;
    fS = cSeconds - tSecond.
    }

    sH = fH + lastcallH;
    sM = fM + lastcallM;
    sS = fS + lastcallM;

    If (< 0 sH: sM < 0 | sS < 0)
    {
    sH = sH * (-1);
    sM = sM * (-1);
    sS = sS * (-1);
    }
    finalMil = (sH * 60) + (sM * 60) + (sM * 60);

    Allows you to implement a timer
    var yahooTime:Timer = new Timer (finalMil, repeat);
    var repeat: Number = 1;

    yahooTime.start ();

    yahooTime.addEventListener (TimerEvent.TIMER, displayCall);

    function displayCall(event:TimerEvent):void
    {
    trace ("well let's success");
    }

    your first trace() statement is almost certainly not what you want.

    dtDate2 is 08/08/2009

    and it's probably not what you want.  Flash months are zero-based.  That is to say, January corresponding to month 0 and December is the month 11.

  • "There is a time difference between the client and the server"

    Unit 4.0.3

    Everything worked very well, and all of a sudden, I'm not able to connect to the server unit using any domain account. When I enter the domain/name username/password, I get this error message:

    ************************************************

    The system is unable to log on due to the following error:

    There is a time difference between the client and the server.

    Try again or contact your system administrator.

    **************************************************

    I can use the same domain account (unityinstall) and the journal in other machines. I can connect the machine to the unit using a local account. There is no time difference between the DC server and unity.

    Need help,

    Thank you

    Partha

    Log on to your LOCAL computer using an account that has privileges

    At the command prompt, type the following:

    NET TIME ancien_mot_passe/set

    Found this on the MS site:

    Cannot open a session if the Date and time are not synchronized

    http://support.Microsoft.com/default.aspx?scid=kb;en-us;232386&product=Win2000

  • What is the main difference between Excel Addin and SmartView?

    Hi all
    What is the difference between Excel Addin and SmartView in Hyperion 11.1.2?

    Thanks in advance

    RSG

    Published by: RSG on March 29, 2011 05:42

    If you want the main difference between the Excel add-in and smartview I'd say it's just they way it connects and retrieves the data. the add-in uses the grid api and tcpip and smartview uses java on the web. There are a ton of differences between the supplement and smartview, but as the versions come thay are close. Here are a few others can add to the list:

    ---------------------------------------------------------------------Add-in------------------------------smartview
    works on several office products - no - yes
    Can connect to
    Essbase-----------------------------------------------------------No---------------------------------------Yes
    HFM----------------------------------------------------------------No---------------------------------------Yes
    Planning----------------------------------------------------------No---------------------------------------Yes
    Can view members and aliases at once - Yes - No
    Leaf specific options - Yes - No
    Great shims formatting - Yes - sometimes

  • What are the practical differences between SEL20F28 and SEL16F28?

    What are the practical differences between SEL20F28 and SEL16F28?

    The 20 is much sharper on the edges and less vignetting.

    The 20 also provides more convenient angle of view walk-around lens.

  • How to read the voltage difference between the 2 pins of a RS232?

    Hi, I'm new in this field and I'm having a problem in the acquisition of data from a RS232 port. In fact, I don't know if it is possible to do.

    What I would do, is to read the voltage difference between the two pins of the port. It's because I want to measure the temperature (and save the data) in a very simple way. I have 6 thermocouples and I thought weld at the different pins of the RS232 interface and acquire analog signals in mV with the computer. Then, I would use a thermometer to calibrate the values.

    Is it possible to do? How? I searched a lot but I can't find any tutorial or similar to this example...

    No, it is not possible to do it with the serial port. The port is not an A/D converter. A simple RS-232 tutorial will tell you that the port is binary in nature.

  • What is the main difference between...

    What is the main difference between the VARIABLE LOCAL, REFERENCE, PROPERTY NŒUD and INVOC. NŒUD?

    Can u show this with an example in a VI using all these?

    (From the help of LV)

    Local variable: http://zone.ni.com/reference/en-XX/help/371361F-01/glang/local_variable/

    Property node: http://zone.ni.com/reference/en-XX/help/371361F-01/glang/property_node/

    Node to call it: http://zone.ni.com/reference/en-XX/help/371361F-01/glang/invoke_node/

    Building VI Server Application: http://zone.ni.com/reference/en-XX/help/371361F-01/lvconcepts/building_vi_server_applications/

    You can also find this information by performing a search in help of LabVIEW on your PC or by conducting a search online help:

    http://zone.NI.com/reference/en-XX/help/371361F-01/

  • What is the major differences between App - V vs vs Med - V VDI?

    Can anyone explain when to use App - V, Med - V or VDI? Or what is the major difference between the two? I used a small App - V.

    Any link/info is welcome
    Thank you

    Hello Remus Rigo,

    I suggest your review link Desktop Virtualization listed here.

    This link will take you to the visit the Desktop Virtualization TechCenter. There is information at this location on Virtual PC, Client light Windows and other virtualization ToolsApp-V, VDI and Med - V. and training.

    For more information, it would be better to post in TechNet for ITPROs.

    Thank you

    Marilyn

  • There are the functional differences between the trial and the full version?

    There are the functional differences between the trial and the paid version for Labview 2009?  Thank you.

    He described the Labview eval version limitations: Limitations of the Evaluation Version of LabVIEW

    See you soon,.

  • The main difference between T400 (2765 - 32G) and T500 (2243 - 64G)

    Hi, someone can describe the main differences between these 2 lenovo GST. I choose between these two. Any advice would be beneficial. Kenneth

    The t400 you watch is CCFL and the T500 is LED backlite, I noticed the LED screens are a little more usable and need to improve your battery a little.

    2 additional questions:

    1 read the info product how can I determine who has the ccfl compared to the led... I tried but have not found.

    2. I also have a t400 6474-wnp is similar to 2765 - 32 g?

    THX kenneth

     

Maybe you are looking for