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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T23:45:23+00:00 2026-06-14T23:45:23+00:00

Trying to write a query in SQL for the following: an event has a

  • 0

Trying to write a query in SQL for the following: an event has a start/end date/time. Users are available for certain times. How can I find the total time a user is available for a particular event?

Example:

--Event--
eventID eventStart          eventEnd
1       2012-10-10 15:00    2012-10-10 18:00

--Available--
userID  availStart          availEnd
64      2012-10-10 10:00    2012-10-10 16:00
64      2012-10-10 16:30    2012-10-10 16:40
64      2012-10-10 16:55    2012-10-10 22:00

The user is free for 135 minutes (60 mins from 15:00-16:00, 10 mins from 16:30-16:40, 65 mins from 16:55-18:00).

Some help writing the SQL would be really helpful, finding this one tricky!

  • 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-14T23:45:24+00:00Added an answer on June 14, 2026 at 11:45 pm

    Tested it now: http://sqlfiddle.com/#!3/a4e7a/2

    I’ve assumed a user table. There’s lots of scope for performance improvement. E.g by adding a where clause that eliminates ranges that don’t overlap. It’ll probably also be faster if you replace the function with a hideous case statement.

    The tricky bit is figuring out the algorithm for how much of two timespans overlap. I always find it useful to draw pictures of the cases:

    Case 1
    |------|
             |=======|
    
    Case 2
    |------|
         |======|
    
    Case 3
    |-------|
      |===|
    

    and the equivalent with the orders reversed.

    It turns out the overlap is the minimum of the two end times minus the maximum of the two start times. (If negative there is no overlap). I always have to check all the cases to re-convince myself of this.

    -- Function that determines how many minutes of overlap there are between two timespans
    Create Function dbo.MinutesOverlap(
      @Start1 as datetime, @End1 as datetime, @Start2 as datetime, @End2 as datetime
    ) Returns int As
    Begin
      Declare 
        @MaxStart As datetime,
        @MinEnd As datetime,
        @Ret int = 0
    
      Set @MaxStart = Case When @Start1 > @Start2 Then @Start1 Else @Start2 End
      Set @MinEnd = Case When @End1 > @End2 Then @End2 Else @End1 End
    
      If @MaxStart < @MinEnd
        Set @Ret = DateDiff(Minute, @MaxStart, @MinEnd)
    
      Return @Ret
    End
    
    
    Select
      u.UserID,
      e.EventID,
      Sum(dbo.MinutesOverlap(e.eventStart, e.eventEnd, a.availStart, a.availEnd))
    From
      Event e
        Cross Join
      User u
        Left Outer Join
      Available a
        On u.UserID = a.UserID
    Group By
      u.UserID, 
      e.EventID
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write an SQL query to retrieve all of a users
I'm trying to write a sql query to the following (from this site http://sqlzoo.net/1b.htm
I am trying to write the following SQL query as a JPA query. The
I am trying to write following SQL query using JPA Criteria API SELECT *
I'm trying to write an SQL query that will gather certain data in one
I am trying to write a linq to sql query equivalent to the following
I am trying to write a sql query like the example below, however, I
I'm trying to write an SQL query that would search within a CSV (or
I am trying to write an SQL query within Visual Studio TableAdapter Query Wizard
I have a SQL query that I'm trying to write, but I'm not quite

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.