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 6741989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:47:24+00:00 2026-05-26T11:47:24+00:00

We have a table called Events, with columns Id(int), EventDate(DateTime), EventStart(datetime) and EventEnd(datetime). All

  • 0

We have a table called Events, with columns Id(int), EventDate(DateTime), EventStart(datetime) and EventEnd(datetime).

All events start and end on a single day (i.e. no events end the next day), however events in a given date may overlap between them (including one of them could cover another entirely).

Any number of events may occur for a given date.

I would like to, for a single day calculate the total duration during which at least one event was running in T-SQL. I can select the events on a given date, and have even written a function returning true if two events are overlapping and false if not.

I am stuck however in how to take the records in pairs and run them through my function, adding the durations appropriately until I run out of events.

Can you help?

Chris

  • 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-05-26T11:47:25+00:00Added an answer on May 26, 2026 at 11:47 am

    Try this:

    --test table
    declare @t table(fromt datetime, tot datetime)
    --test data
    insert @t values('2011-01-01 10:00', '2011-01-01 11:00')
    insert @t values('2011-01-01 10:00', '2011-01-01 10:05')
    insert @t values('2011-01-01 10:30', '2011-01-01 11:30')
    insert @t values('2011-01-01 12:00', '2011-01-01 12:30')
    insert @t values('2011-01-02 12:00', '2011-01-02 12:30')
    
    --query
    ;with f as
    (
        select distinct fromt from @t t 
        where not exists(select 1 from @t where t.fromt > fromt and t.fromt < tot)
    ), t as
    (
        select distinct tot from @t t 
        where not exists(select 1 from @t where t.tot >= fromt and t.tot < tot)
    ), s as
    (
        select datediff(day, 0, fromt) d, datediff(second, fromt, (select min(tot) 
          from t where f.fromt < tot and datediff(day, f.fromt, tot) = 0)) sec 
        from f
    )
    select dateadd(day, 0, d) day, sum(sec)/60 [minutes]
    from s
    group by d
    order by d
    

    Result:

    day                     minutes
    ----------------------- -------
    2011-01-01 00:00:00.000 120
    2011-01-02 00:00:00.000 30
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Assuming I have table called events with the columns INT id DATETIME start_time DATETIME
We have a table called table1 ... (c1 int indentity,c2 datetime not null,c3 varchar(50)
So I have an example table called items with the following columns: item_id (int)
I have a Table, called Table1, with 3 columns: 1) id - An integer
I have a table called events with 5 fields. Among those fields I have
I have a table called Events that I want to insert information to. Here
I have two tables called 'events' and 'topics' each table can have many comments.
I have two tables, one called Events and one called Documents. Each table has
I have a table called teams with column inactive and another table events with
I have a table called Event. A column has duration--which is...how long that event

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.