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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T06:43:49+00:00 2026-05-18T06:43:49+00:00

Two temp tables are created and then loaded…Here’s the schema. Create table #SH ([date]

  • 0

Two temp tables are created and then loaded…Here’s the schema.

Create table #SH ([date] datetime,
        sched_id int,
        agent_id int)

Create table #SD (sched_id int,
        start_minute int,
        length int,
        exception_code int)

(Schema and design is something I can’t change unfortunately, both temp tables are loaded from flat files. I can introduce and load new temp tables if needed).

A little background – The #SH header table holds a persons schedule as ‘Start_minute’ and goes for ‘schedule_length’ in minutes. For example, if start minute and schedule length were both 480, that would read as 8am (8am = 480th minute) and goes until 4pm (480 minutes later, 4pm = 960th minute)

The #SD table holds exceptions to the header. In the example above, the person would likely have a lunch exception which would be start_minute = 720 and length of 30 (12:00 – 12:30).

Date and agent_id is the only thing I’m interested out of #SH, the exception info in #sd is what I’m interested in.

This query works:

Select [date],#sd.start_minute,#sd.length,#sd.start_minute + #sd.length as 'end_minute',agent_id
from #SH 
inner join #SD on #SD.sched_id = #sh.sched_id

*end_minute is ultimately a calculated value of start+length = end

This returns something like:

   Date     Start  length   end

1 2010-11-11 600    30  630

2 2010-11-11 630    40  670

3 2010-11-11 750    15  765

4 2010-11-11 800    40  840

Now I wish I could say this was over and walk away…but data entry issues exist. In line 1 and 2, the end time of line 1 lines up with the start time in line 2 and should be combined so my results look like this :

Date     Start  length     end

1 2010-11-11 600    70  670

2 2010-11-11 750    15  765

3 2010-11-11 800    40  840

Any idea’s on how to build this logic so I get the 3 lines back instead of 4? I’m working on joining the table to itself on #sd1.start + #sd1.length = #sd2.start for now.

And to further complicate…the example above was 2 lines that needed combined. I’ve come across a record that had 30 1 minute entries in succession that I will need to make into a single record. Fortunately they cannot overlap (you won’t have 2 records occupying the same minutes), but I don’t think the join statement I’m considering above will work for that.

  • 1 1 Answer
  • 1 View
  • 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-18T06:43:49+00:00Added an answer on May 18, 2026 at 6:43 am

    No need for a CTE, all you need is a helper table. Create it once, like so:

    Create Table DayMinute(Minute Integer)
    Declare @M Integer
    Set @M = 1
    While (@M <= 24*60)
    Begin
      Insert Into DayMinute(Minute) Values(@M)
      Set @M = @M + 1
    End
    

    Then, all you need is a bit of tricksiness:

    Select 
      DM.Minute,
      SD.Sched_ID
    Into #MinutesWithException
    From 
      DayMinute As DM
      Inner Join #SD As SD
        On DM.Minute Between SD.Start_Minute And SD.Start_Minute + Length
    
    Select
      MWE.Sched_ID,
      SH.[Date],
      SH.Agent_ID,
      [Start_Minute] = MWE.Minute,
      [End_Minute] = (Select Min(Last.Minute) -- First one to have no successor
                      From #MinutesWithException As Last
                      Where Last.Sched_ID = MWE.Sched_ID
                        And Last.Minute > MWE.Minute
                        And Not Exists(Select *
                                       From #MinutesWithException As Next
                                       Where Next.Sched_ID = MWE.Sched_iD
                                         And Next.Minute = Last.Minute + 1))
    From 
      #MinutesWithException As MWE
      Inner Join #SH As SH
        On MWE.Sched_ID = SH.Sched_ID
    Where
      Not Exists(Select * -- All those without predecessor
                 From #MinutesWithException As Previous
                 Where Previous.Sched_ID = MWE.Sched_ID
                   And Previous.Minute = MWE.Minute - 1)
    

    Remember, a lot of SQL problems can be solved by rephrasing them. Don’t ask “which ranges do not have a gap”, ask “which minutes have an interval”. The rest follows from there.

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

Sidebar

Related Questions

I created two temp tables, one table is for orders and the other is
Table Schema For the two tables, the CREATE queries are given below: Table1: (file_path_key,
Within PHP I do: 1.) A temporary table is created: CREATE TEMP TABLE new_table
I have two tables in MySQL sales database: Orders table: CREATE TABLE salestest.`orders` (
I have two temp tables, both populated by TSQL queries. Temp Table A containing
Two tables. I have column a (datetime type) and column b (numeric type) in
I am trying to compare two tables to find rows in each table that
I have created table #temp with columns id as int identity(1,1) and name as
I get this very nondescript error trying to create my next two tables, I
I have two tables. One is a table of the reports that have been

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.