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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:37:44+00:00 2026-05-23T23:37:44+00:00

I have a table(begin, end, typeid) with time intervals, something like this: … 14:10:44

  • 0

I have a table(begin, end, typeid) with time intervals, something like this:

...
14:10:44    14:15:51  1
14:12:33    14:18:42  2
15:24:09    15:24:17  1
...

There is some rules: intervals with type id 2 intersepts with intervals of type 1.
And after processing I get something like this:

...
14:10:44    14:18:42 <-- as a result of merging
15:24:09    15:24:17 
...

Is there exists any tehnics or methods to work with such data? May be there exists any usefull representation of intervals of time?

P.S. SQL Server 2008 R2

  • 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-23T23:37:45+00:00Added an answer on May 23, 2026 at 11:37 pm

    It is really hard to code up against data like yours. I have seen it a couple of times in real life situations. Here are 2 ideas.

    declare @t table(low time, high time, type_id tinyint)
    
    insert @t values('14:10:44','14:15:51', 1)
    insert @t values('14:12:33','14:18:42', 2)
    insert @t values('15:24:09','15:24:17', 1)
    
    select low, coalesce(a.high, t.high) high 
    from @t t 
    cross apply (select min(high) high 
    from @t t2 
    where high > t.high and type_id = 2  
    and not exists (select 1 from @t 
    where t2.high between low and high and type_id = 2 and not 
    (t2.high = high and t2.low = low))) a
    where type_id = 1
    

    Here is an alternative way. May even be better, depending on your data.

    ;with cte as 
    (
    select low low2, high, low 
    from @t where type_id = 1 
    union all
    select cte.high, t.high, cte.low
    from @t t 
    join cte on cte.high > t.low 
    and cte.high < t.high 
    where t.type_id = 2
    )
    select low, max(high) high  from cte group by low
    

    I am assuming that the low with type_id 1 is always the lowest in the set. I am also assuming there are no overlapping sets for rows with type_id 1 (and their subrows) since that would make no sense at all.

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

Sidebar

Related Questions

I have a LaTeX table that looks like this: \begin{table}[!ht] \centering \small \caption{ \bf{Caption}}
I have table with some fields that the value will be 1 0. This
I'm new to triggers . I have table like this CREATE TABLE [dbo].[Positions]( [Id]
I have this sequence of queries: begin execute immediate 'drop table mytable1'; exception when
Suppose I have a table (MySQL) like this: CREATE TABLE sessions ( session_id INT
I have a table MyTable with a trigger defined like this: ALTER TRIGGER [MyTableInsertDeleteUpdate]
Let's say we have a table \begin{tabular}{|c|c|} \begin{sideways}A\end{sideways}&\begin{sideways}B\end{sideways}\\\ a & b \\\ \end{tabular} How
I have table with 50 entries (users with such details like Name Surname Location
I have table with a unique auto-incremental primary key. Over time, entries may be
I have a table as shown in this figure: The LaTeX code for the

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.