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

  • SEARCH
  • Home
  • 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 8026061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T23:21:53+00:00 2026-06-04T23:21:53+00:00

Check out my SQLFiddle here ( link ) In SQL Server 2008, I have

  • 0

Check out my SQLFiddle here (link)

In SQL Server 2008, I have table of event start and end times, like the image below.

enter image description here

I need to write a query that allows me to figure out how much of each event fell into which work shift. Our shifts are 12 hrs long and go from 06:00-18:00 and 18:00-06:00.

The query should produce results like the image below.

enter image description here

From that, I can then figure out total event durations for a particular work shift.

What can I do to go from the first image, to the second image?

  • 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-04T23:21:55+00:00Added an answer on June 4, 2026 at 11:21 pm
    With Shifts As
      (
        Select 1 As Num
          , Cast('2012-05-01 6:00 AM' As datetime) As ShiftStart
          , DateAdd(hh,12,Cast('2012-05-01 6:00 AM' As datetime)) As ShiftEnd
        Union All
        Select Num + 1, ShiftEnd, DateAdd(hh,12,ShiftEnd)
        From Shifts
        Where ShiftEnd < '2012-05-30'
        )
      , Segments As
        (
        Select event_id
          , Case 
            When Shifts.ShiftStart > event_start Then Shifts.ShiftStart 
            Else event_start
            End As start_split_segment        
          , Case 
            When Shifts.ShiftEnd < event_end Then Shifts.ShiftEnd
            Else event_end
            End As end_split_segment          
          , Count(*) Over ( Partition By E.event_id ) As SegmentCount
        From events As E
          Join Shifts
            On E.event_start <= ShiftEnd
              And E.event_end > ShiftStart
          )
    Select E.event_id, E.description, E.event_start, E.event_end
      , S.start_split_segment, S.end_split_segment
      , Case When SegmentCount > 1 Then 1 Else 0 End As is_split
      , NullIf(SegmentCount,1) As split_segments
    From Segments As S
      Join events As E
        On E.event_id = S.event_id
    

    SQL Fiddle version

    In this solution, I generated a calendar of every shift’s start and end date. You can extend the calendar by changing Where ShiftEnd < '2012-05-30' to a larger date. Note that if you end up with more than 50 shifts or so, you’ll want to add Option(Maxrecursion 0); to end of the query to lift SQL Server’s cap.

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

Sidebar

Related Questions

Check out this pic of my SQL 2008 Management Configuration screen for TCP: I
Please check out my code here http://jsfiddle.net/bYDPr/20/ So I have some HTML textarea elements
Check out this jsbin . I have a form with a custom button that
Can check out here alt text http://51hired.com/static/problem.bmp
Check out the following screenshot: http://www.jesserosenfield.com/beta/descenders.png My problem is that descending (like the 7
Check out this page from the New York Times: http://homedelivery.nytimes.com/HDS/learnMorePopUp.do ?mode=common.learnMorePopUp &productId=NDS &prodRate=7.40 I
Please check out the jsfiddle link below: http://jsfiddle.net/H49EC/1/ As you can see it is
Please check out the jsFiddle link below: http://jsfiddle.net/pnpHS/4/ As you can see its a
Check out this code : <a href=# id=link>Link</a> <span>Moving</span> $('#link').click(function () { console.log(Enter); $('#link').animate({
Check out code here ! These are my problems: Div won't toggle to hide

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.