How to calculate hours worked, excluding the time overlapping.

Hello gurus,


I need like calculating the gain for the hours worked on different projects. But I don't want to pay twice if they have overlapping of the work period.

I was wondering if someone can suggest a better way to calculate the hours.

I have a data in the following format:
select 1111 as person_id, '01-AUG-11' as event_date, to_date('01-AUG-11 18:00', 'DD-MON-YY Hh24:MI') as start_time, to_date('02-AUG-11 06:00', 'DD-MON-YY Hh24:MI') as end_time, 'AAA' project_id from dual union all
select 1111 as person_id, '02-AUG-11' as event_date, to_date('02-AUG-11 03:00', 'DD-MON-YY Hh24:MI') as start_time, to_date('02-AUG-11 06:00', 'DD-MON-YY Hh24:MI') as end_time, 'BBB' project_id from dual union all
select 1111 as person_id, '02-AUG-11' as event_date, to_date('02-AUG-11 05:00', 'DD-MON-YY Hh24:MI') as start_time, to_date('02-AUG-11 08:00', 'DD-MON-YY Hh24:MI') as end_time, 'CCC' project_id from dual union all
select 1111 as person_id, '03-AUG-11' as event_date, to_date('03-AUG-11 11:00', 'DD-MON-YY Hh24:MI') as start_time, to_date('03-AUG-11 15:00', 'DD-MON-YY Hh24:MI') as end_time, 'AAA' project_id from dual union all
select 2222 as person_id, '02-AUG-11' as event_date, to_date('02-AUG-11 18:00', 'DD-MON-YY Hh24:MI') as start_time, to_date('03-AUG-11 08:00', 'DD-MON-YY Hh24:MI') as end_time, 'AAA' project_id from dual union all
select 2222 as person_id, '03-AUG-11' as event_date, to_date('03-AUG-11 01:00', 'DD-MON-YY Hh24:MI') as start_time, to_date('03-AUG-11 10:00', 'DD-MON-YY Hh24:MI') as end_time, 'BBB' project_id from dual  union all
select 3333 as person_id, '03-AUG-11' as event_date, to_date('03-AUG-11 01:00', 'DD-MON-YY Hh24:MI') as start_time, to_date('03-AUG-11 10:00', 'DD-MON-YY Hh24:MI') as end_time, 'CCC' project_id from dual 
I would like to output following the path:
select 1111 as person_id,     '01-AUG-11' as event_date,     '01-AUG-11'     as start_time,'02-AUG-11'     as end_time,'AAA' as project_id, 12 as hours from dual  union all
select 1111,     '02-AUG-11',     '02-AUG-11'     ,'02-AUG-11'     ,'BBB', 0 as hours from dual  union all /*overlaps with row 1 */
select 1111,     '02-AUG-11',     '02-AUG-11'     ,'02-AUG-11'     ,'CCC', 2 as hours from dual  union all /* overlaps with row 1 upto 06:00 AM */
select 1111,     '03-AUG-11',     '03-AUG-11'     ,'03-AUG-11'     ,'AAA', 4 as hours from dual  union all  /* No overlaps */
select  2222,     '02-AUG-11',     '02-AUG-11'     ,'03-AUG-11'     ,'AAA', 14 as hours from dual  union all 
select  2222,     '03-AUG-11',     '03-AUG-11'     ,'03-AUG-11'     ,'BBB', 2 as hours  from dual   union all /*Overlap with row 5 upto 6:00 AM*/
select  2222,     '03-AUG-11',     '03-AUG-11'     ,'03-AUG-11'     ,'CCC', 0 as hours  from dual   /* complete overlaps with row 6 */
I don't like how they are showing on each line and the hours are calculated. My only requirement is that calculated hours aren't twice if there is overlap for each person.

I really appreciate it if someone can help me.
Thank you.
with data as (
select 1111 as person_id, '01-AUG-11' as event_date, to_date('01-AUG-11 18:00', 'DD-MON-YY Hh24:MI') as start_time, to_date('02-AUG-11 06:00', 'DD-MON-YY Hh24:MI') as end_time, 'AAA' project_id from dual union all
select 1111 as person_id, '02-AUG-11' as event_date, to_date('02-AUG-11 03:00', 'DD-MON-YY Hh24:MI') as start_time, to_date('02-AUG-11 06:00', 'DD-MON-YY Hh24:MI') as end_time, 'BBB' project_id from dual union all
select 1111 as person_id, '02-AUG-11' as event_date, to_date('02-AUG-11 05:00', 'DD-MON-YY Hh24:MI') as start_time, to_date('02-AUG-11 08:00', 'DD-MON-YY Hh24:MI') as end_time, 'CCC' project_id from dual union all
select 1111 as person_id, '03-AUG-11' as event_date, to_date('03-AUG-11 11:00', 'DD-MON-YY Hh24:MI') as start_time, to_date('03-AUG-11 15:00', 'DD-MON-YY Hh24:MI') as end_time, 'AAA' project_id from dual union all
select 2222 as person_id, '02-AUG-11' as event_date, to_date('02-AUG-11 18:00', 'DD-MON-YY Hh24:MI') as start_time, to_date('03-AUG-11 08:00', 'DD-MON-YY Hh24:MI') as end_time, 'AAA' project_id from dual union all
select 2222 as person_id, '03-AUG-11' as event_date, to_date('03-AUG-11 01:00', 'DD-MON-YY Hh24:MI') as start_time, to_date('03-AUG-11 10:00', 'DD-MON-YY Hh24:MI') as end_time, 'BBB' project_id from dual  union all
select 2222 as person_id, '03-AUG-11' as event_date, to_date('03-AUG-11 01:00', 'DD-MON-YY Hh24:MI') as start_time, to_date('03-AUG-11 10:00', 'DD-MON-YY Hh24:MI') as end_time, 'CCC' project_id from dual
)
--
-- end of test data
--
select
person_id, start_time, end_time, project_id,
(end_time - start_time) * 24 hours,
greatest((
  end_time - greatest(start_time, nvl(lag(end_time) over (partition by person_id order by start_time,end_time),start_time))
) * 24, 0) hours_no_overlap
from data
order by
person_id, start_time, end_time
;

With the [http://download.oracle.com/docs/cd/E11882_01/server.112/e17118/functions082.htm#i1327527 url] analytical function GAL, you can get what was the time of the end of the previous row. Greatest() function then takes the value of a line start_time or end_time from previous row, whichever is the greater.

It is a way to do that is fairly simple. If you have multiple overlaps, so it may not work completely in all cases. But maybe it can serve as a starting point for you ;-)

Tags: Database

Similar Questions

  • Tecra A9: How I get saved keys FN-esse to work all the time?

    How can I get my saved keys FN-esse to work all the time?

    It is without having to use the Toshiba Assist button to bring up the Fn-esse chart on my desk?

    Hi Cameillia

    Please excuse my question, but what do you do exactly?
    You want to use the FN + F1 - F9 button functionality without using the FN key?

    Sorry, but I think that it is not possible

    But if you use Vista OS, then you can use the mouse cursor and might point up, display area and could activate the Vista FlashCards.
    The FlashCards support the same functionality as the use of the keys FN + F1 - F9

    Concerning

  • My screen saver does not work all the time, usually this seems to happen once the aircraft stopped in the energy saving mode.

    Original title: my screensaver does not work all the time

    My screen saver does not work all the time.  First time I have turn on computer, it works OK.  Later in the day, this isn't.  Usually, this seems to happen once the aircraft stopped in the energy saving mode.  After the reactivation of the computer (do not), the screen saver does not work when it should. How to run all the time?  I am running Kapersky Internet Security 2011.  This would be closed by the screen saver?
    Thanks, Garybuyit

    Hi garybuyit,

    Thanks for posting your question in the Microsoft answers Forums.

    Just so I'm clear, updated you the graphics cards in Device Manager? You have access to a Windows XP installation disc. If so, follow the steps in this document to make a scan of the SFC. This analysis will attempt to repair or replace any missing or corrupted system files.

  • Computer is very slow and IE does not work all the time.

    I bought a computer from my old employeer.  He is doing all sorts of things, or should I say not to do all sorts of things.  It is very slow and IE does not work all the time.  They have questions arise and ask if the administrator oks things and other things like that.  It came with any disks.  I would like to wipe, clean and load a new disc, but I don't know where to find the disc or if I can do it.   Can someone please?

    Hello

    Proceed as follows to contribute to these two questions. You can probably buy the disks of Vista machine to system or get
    the former employer to do it for you if necessary.

    Try these and it can also be a good idea to update your main drivers later. The General corruption can also play a
    role in this issue. And even the TCP/IP stack might need to be refreshed.

    What antivirus/antispyware/security products do you have on the machine? Be one you ALREADY had on this
    machine, including those you have uninstalled (they leave leftovers behind which can cause strange problems).

    If no joy there's something blocking perhaps.

    Start - All Programs - Accessories - System Tools - IE with no Addons - what works best?

    IE - Tools - Internet Options - Advanced - tab click on restore, and then click Reset - apply / OK

    IE - Tools - Internet Options - Security tab - click on reset all default areas - apply / OK

    Close and restart IE or IE with no addons

    not better?

    IE - tools - manage Addons (for sure disable SSV2 if it is there, it is no longer necessary but Java always install
    "(and it causes problems - you never update Java to go back in and turn it off again)." Search for other possible problems.

    Windows Defender - tools - software explore - look for problems with programs that do not look right. Permit
    are usually OK and "unauthorized" are not always bad. If in doubt about a program to ask about it here.

    Could be that a BHO - BHOremover - free - standalone program, needs no installation, download and run - not all
    are bad, but some can cause your question (toolbars are BHO).
    http://securityxploded.com/bhoremover.php

    Startup programs
    http://www.Vistax64.com/tutorials/79612-startup-programs-enable-disable.html

    Also get Malwarebytes - free - use as scanner only.

    http://www.Malwarebytes.org/

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

    Try these to erase corruption and missing/damaged file system repair or replacement.

    Run DiskCleanup - start - all programs - Accessories - System Tools - Disk Cleanup

    Start - type in the search box - find command top - RIGHT CLICK – RUN AS ADMIN

    sfc/scannow

    How to analyze the log file entries that the Microsoft Windows Resource Checker (SFC.exe) program
    generates in Windows Vista cbs.log
    http://support.Microsoft.com/kb/928228

    Then, run checkdisk - schedule it to run at next boot, then apply OK your way out, then restart.

    How to run the check disk at startup in Vista
    http://www.Vistax64.com/tutorials/67612-check-disk-Chkdsk.html

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

    Departure - in the search box, type-> order

    at the top of the list to find COMMAND - CLICK RIGHT to it - RUN AS ADMIN

    Type the following commands (or copy and paste one at a time), each followed by pressing on enter.

    ipconfig/flushdns

    nbtstat-r

    nbtstat - RR

    netsh int Reinitialis

    netsh int ip reset

    netsh winsock reset

    RESET

    That resets your TCP/IP stack

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

    Maybe need of these:

    For drivers, visit manufacturer of emergency system and of the manufacturer of the device that are the most common.
    Control Panel - Device Manager - Display Adapter - note the brand and complete model of your
    video card - double - tab of the driver - write version information. Now click on UPdate Driver (this
    cannot do anything as MS is far behind the certification of drivers) - then do a right click - Uninstall - REBOOT
    This will refresh the driver stack.

    Repeat this for network - card (NIC), Wifi network, sound, mouse, and keyboard if 3rd party with their
    own the software and drivers and all other main drivers that you have.

    Now go to the site of the manufacturer of system (Dell, HP, Toshiba as examples) (restoration) and then of the manufacturer of the device
    (Realtek, Intel, Nvidia, ATI, for example) and get their latest versions. (Look for the BIOS, Chipset and software)
    updates on the site of the manufacturer of the system here.)

    Download - SAVE - go to where you put them - right click - RUN AD ADMIN - REBOOT after each installation.

    Repeat to the manufacturers - BTW on device at the DO NOT RUN THEIR SCANNER - manually check by model.

    Look at the sites of the manufacturer for drivers - and the manufacturer of the device manually.
    http://pcsupport.about.com/od/driverssupport/HT/driverdlmfgr.htm

    How to install a device driver in Vista Device Manager
    http://www.Vistax64.com/tutorials/193584-Device-Manager-install-driver.html

    If you update the drivers manually, then it's a good idea to disable the facilities of driver in the Windows updates,
    This leaves ONE of Windows updates, but it will not install the drivers who are generally older and cause
    questions. If updates offers a new driver and then hide it (right click on it) and then go look for new ones
    manually if you wish.

    How to disable automatic driver Installation in Windows Vista - drivers
    http://www.AddictiveTips.com/Windows-Tips/how-to-disable-automatic-driver-installation-in-Windows-Vista/
    http://TechNet.Microsoft.com/en-us/library/cc730606 (WS.10) .aspx

    Hope these helps.

    Rob - bicycle - Mark Twain said it is good.

  • I have iphone5s and my contact id does not work, all the time he show faild Setup touch id please help?

    I have iphone5s and my contact id does not work, all the time he show faild Setup touch id please help?

    Hi yjain51,

    I understand you had some problems recently with getting Touch ID to work reliably on your iPhone 5s. I know that it is a feature of great security for your iPhone, so I'm happy to help you.

    When you use Touch ID, make sure that your hands are free of any dirt or moisture so that fingerprints can be read clearly. This article explains further:

    Use the ID Touch on iPhone and iPad - Apple Support

    Thank you for using communities Support from Apple. See you soon!

  • How to return an iphone at the time where ordered by mistake?

    How to return an iphone at the time where he ordered by mistake?

    rblilbob wrote:

    How to return an iphone at the time where he ordered by mistake?

    Go back where you bought it from.

  • My driver support does not work all the time, won't let me connect, keeps losing internet connection.

    * Original title: SUPPORT of the PILOTS LOSE INTERNET.

    My driver support does not work all the time, won't let me connect, keeps losing internet connection.

    What do you mean by "driver support?

    If you have installed one of these offers of support software driver, you have installed malware.  The vast majority of them no useful software and installing a lot of malware on your computer.

    The drivers are not the soft thing you should normally be updated.  I recommend the updated drivers only in the case of a problem that has to solve, and it's rare.

    Is the ONLY source, you should consider for driver updates or OEM support site.  Even those who sometimes go way overboard in trying to update the drivers.  The latest Dell systems are equipped with updated software that checks for new updates every day.   In most cases, drivers are rarely updated after the first year of a product.

  • Why the clock on my calendar seems to be working at the time of the West Coast, while my ThunderBird is defined and works well at the time of the coast is?

    Ladies and gentlemen:

       I recently started using Calendar add-on to Thunderbird. My test event entered okay. But, my invite showed up on my colleague's PC with meeting time 3 hours late. That is, my meeting time is 11:00 Eastern time. But, he got the time as 14:00 which was 11:00 West coast time. I checked my "Calendar" tab view. There is a tell-tale sign that a red horizontal line that appears to follow my PC's real time clock but lagging 3 hours behind. So far, I could not find how to synchronize them.
    
       My PC was used on the West coast for several years before I brought it to the East coast a few months ago. Since then, my Thunderbird appears to run fine with the clock setting changed to the East coast time zone. Could there be any kind of memory somewhere that took my numerical 11:00, but recorded it by tagging as a West coast time zone entry?
    

    Abe (2015-10-11 11:54)

    Make sure that you do not have the correct time zone set lightning (calendar).

    In the top right of the Thunderbird window, click the menu button > Options > Options > calendar > general tab > zone

  • 12 hours to convert the time format to 24-hour time format

    Hello

    The following query does not work why?

    Select To_date ('10:35 PM', 'HH24:MI') From DUAL
    

    It gives the following error

    date format picture ends before converting all of the input string.

    but the following query works fine

    Select To_date ('10:35', 'HH24:MI') From DUAL


    I need to convert the format of the time in the first motion HH24 in PLSQL, how can I do this?

    Thank you


    Thank you youHH24:MI

    1 * Select To_date (' 22:35 ', ' Hh: mi AM "), TO_CHAR (To_date (' 22:35 ', ' HH: mi AM'), 'YYYY-MM-DD HH24:MI:SS') From DUAL

    SQL > /.

    TO_DATE (' TO_CHAR (TO_DATE('10))

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

    01-SEPT.-15 2015-09-01 22:35

  • How is automatically update works in the AIR 2.5?

    Hello

    We are about to deploy the runtime Adobe AIR 2.5 in our Organization.

    To do this, we use the following command line

    AdobeAIRInstaller.exe - silent - eulaAccepted

    It works very well. But I don't know how to disable the automatic updates.

    When you read the Adobe AIR configuration guide, http://help.adobe.com/en_US/air/admin/WS485a42d56cd1964167ea49bd124ef17d52a-7ff5.html it says I should create the HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Adobe\AIR\UpdateDisabled registry key and set it to 1.

    But when I run the AIR SettingsManager application after I put this registry key, it is said that the automatic updates are enabled.

    And if I chose to disable the automatic updates of the tool creates the file %APPDATA%\Adobe\AIR\updateDisabled.

    How can I make sure that the automatic updates are disabled for all users on all computers we deploy the runtime Adobe AIR 2.5 for?

    Since we have an enterprise deployment is not really an option to ask each user to run the class SettingsManager AIR whenever they were able to connect a new computer

    Best regards Erik

    Hi Erik,

    The HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Adobe\AIR\UpdateDisabled parameter to 1 will disable TIME updates for all users.  Add the %APPDATA%\Adobe\AIR\updateDisabled file will disable the updates on a PER-user basis.

    Hope this helps,

    Chris

  • How can I maintain lanpscape all the time?

    Hi, I want to keep (the screen not camera) mode of landscape all the time. I tried to disable the automatic rotation and it go to protrate mode immediately. Why don't we have a landscape mode? Or we simply isn't pure auto rotation mode?

    @Hosais

    The only way Sony Home Launcher will continue to run, is that if you are using a Dock or a magnetic cable, other than that, it won't work unless you use a third-party thrower.

  • How to hide pane tools all the time?

    I can go to view > show/hide > tool pane to hide the side window.  How can I configure it to hide all the time?

    Hi samw60634542,

    It is currently not a setting to turn off all the time. Please feel free to submit a request via this form, so that developers can take to review: Adobe - feature request/Bug Report Form.

    Best,

    Sara

  • How we prevent ESX to change the time of BIOS when it starts?

    I have various difficulties becoming NTP is working on 2 new ESX hosts.

    I tried several things, and one of the issues seems to be that ESX on startup changing time system, and thus all the following which is off LeRiz.

    Is there a way I can avoid this?

    I know there is a one line can be/etc/ntp.conf, something like restrict notrap noquery etc, but it shows long after starting the server.

    I know what is happening because when I reboot I can look through ILO and see in the BIOS time system has changed and I have to reset it.

    I'll try to re - install ESX servers and practice with it.

    These are HP servers allowing the setting of the time in UTC.

    I found what MAY be the best procedure for me to get the correct time zone, even though it is old:

    http://KB.VMware.com/selfservice/microsites/search.do?cmd=displayKC & docType = kc & externalId = 1436 & sliceId = 1 & docTypeID = DT_KB_1_1 & dialogID = 8965316 & StateID = 0% 200% 208963389

    KB #1436

    All our servers are in the State of New York, UTC is not absolutely essential for me.

    Thank you, Tom

    chkconfig--level 345 ntpd on

    hwclock--systohc

    Here is an another good KB

    http://KB.VMware.com/kb/1339

  • Why my A30-S203 fans work all the time?

    Hello everyone,

    Fans of my A30-S203 always working at the intermediate level. In fact my average CPU usage is not more 3 percent. But these fans never cease.
    What could be the problem? This sound is really annoying me. And I think that it could also cause a hardware fault.

    Thank you...
    Tunc

    Hello

    Have you checked the settings of the power management utility?
    As far as I know there is an option called cooling method.
    Please change this option in Mid performance mode or in silent mode.

    Good bye

  • Preview only works if the time line is not visible

    I'm having a problem with preview.  My computer plays smoothly if the time line for this model is not visible.  I have to hit play then click the Render what tab or close the window of time line all together.   Why is this happening?

    Assuming you are using El Capitan, Dave is right. It is a known problem. It is listed on this page and a few other places as well. You have found a workaround. Another workaround solution found by the mofxtv user (in this thread) is docked to another court under your control panel mounting.

    Like this:

    Some people use other panels that they use less often.

Maybe you are looking for