Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8387703
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T18:17:13+00:00 2026-06-09T18:17:13+00:00

I have a database table with a Begindate (datetime) and Enddate (datetime). This table

  • 0

I have a database table with a Begindate (datetime) and Enddate (datetime). This table contains activity durations. Now I am trying to create a function to calculate the total duration based on non-overlapping activities within a specified begindate-enddate.

An exaple would be as follow:

I am calling upon this function giving the parameters:
Begindate=’2012-08-16 10:00′
Enddate=’2012-08-16 18:00′

Now let’s say the following data is in the table:

  1. begin: ‘2012-08-15 10:00’ end ‘2012-08-15 14:00’ (total 4 hours)
  2. begin: ‘2012-08-16 09:00’ end ‘2012-08-16 11:00’ (total 2 hours)
  3. begin: ‘2012-08-16 10:30’ end ‘2012-08-16 10:45’ (total 15 minutes)
  4. begin: ‘2012-08-16 12:00’ end ‘2012-08-16 16:00’ (total 4 hours)
  5. begin: ‘2012-08-16 13:00’ end ‘2012-08-16 17:00’ (total 4 hours)
  6. begin: ‘2012-08-16 16:30’ end ‘2012-08-16 16:45’ (total 15 minutes)
  7. begin: ‘2012-08-16 17:30’ end ‘2012-08-16 20:00’ (total 2 hours and 30 minutes)

Now I would love the following to happen:

  1. The first activity is outside the specified period so will not be included.
  2. Partial match so this will return 1 hour.
  3. Activity is inside the second activity so will not be included.
  4. 4 hours withing the period will be included.
  5. 1 hour will be included from 16:00 to 17:00
  6. Activity will not be included.
  7. Only 30 minutes are inside the specified period so that will be included.

The total returned will be: 6 hours and 30 minutes.

If anyone could help me with this I would be very grateful! 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T18:17:14+00:00Added an answer on June 9, 2026 at 6:17 pm

    It might be easier to calculate missing ranges and then substract durations from complete period:

    declare @begindate datetime = '20120816 10:00:00'
    declare @enddate datetime = '20120816 18:00:00'
    
    ; with supplement as (
      select begindate, enddate
        from daterangetable
          -- overlapping ranges only
       where begindate <= @enddate
         and enddate >= @begindate
      union all
      -- empty start range to catch missing start intervals
      select @begindate, @begindate
      union all
      -- empty end range to catch missing end intervals
      select @enddate, @enddate
    )
    select datediff (minute, @begindate, @enddate)
         - sum (datediff (minute, gapStart, gapEnd)) Minutes
    from
    (
      select max(d2.enddate) gapStart, 
             d1.begindate gapEnd
        from supplement d1
       cross join supplement d2
          -- d2 range must start before d1 range
       where d2.begindate < d1.begindate
       group by d1.begindate
          -- Range is completely covered if d2 ends after d1 begins
          -- so it should be removed
      having max(d2.enddate) < d1.begindate
    ) missingRanges
    

    Two ranges overlap if end of first one is after start of second one and start of first one is before end of second one. See the link for (much better) explanation.

    If you are not using Sql Server 2005 or newer, you will need to move code inside with into derived tables for supplement d1 and supplement d2.

    And here is Sql Fiddle with example.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have database table like this +-------+--------------+----------+ | id | ip | date |
I have a database table named Categories . I want to create a form
I have one database table which contains 8 columns. One of the columns is
I have a database table using an enum. This is already working with hibernate
I have a table in an Openoffice Database, containing two date columns. I'm trying
I have an Oracle 10g table that contains a # of log records. This
I have a database table with a DATETIME column. What I want to do
I have a database table, 'Reports'. Each column in this table is of type
I have a database table like this: id | check_number | amount 1 |
I have database table: DROP TABLE translation_en_lt; CREATE TABLE translation_en_lt ( id INTEGER, lt_translation

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.