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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T03:17:10+00:00 2026-06-08T03:17:10+00:00

I was wondering if someone could help me with this SQL statement? Say, I

  • 0

I was wondering if someone could help me with this SQL statement?

Say, I have an SQL Server 2008 table like this:

id -- INT PRIMARY KEY
dtIn -- DATETIME2
dtOut -- DATETIME2
type -- INT

id  dtIn    dtOut    type
1   05:00   10:00    1
2   08:00   16:00    2
3   02:00   08:00    1
4   07:30   11:00    1
5   07:00   12:00    2

I need to remove any time overlaps in the table above. This can be illustrated with this diagram:
enter image description here

So I came up with this SQL:

UPDATE [table] AS t 
SET dtOut = (SELECT MIN(dtIn) FROM [table] WHERE type = t.type AND t.dtIn >= dtIn AND t.dtIn < dtOut) 
WHERE type = t.type AND t.dtIn >= dtIn AND t.dtIn < dtOut

But it doesn’t work. Any idea what am I doing wrong here?

****EDIT****

OK, it took me awhile to get to this. Seems to be a working SQL for what I need it for:

--BEGIN TRANSACTION;

--delete identical dtIn
DELETE dT1
FROM tbl dT1
WHERE EXISTS
(
    SELECT *
    FROM tbl dT2
    WHERE dT1.Type = dT2.Type
    AND dT1.dtIn = dT2.dtIn
    AND (
            dT1.dtOut < dT2.dtOut
            OR (dT1.dtOut = dT2.dtOut AND dT1.id < dT2.id)
        )
);

--adjust dtOuts to the max dates for overlapping section
UPDATE tbl
SET dtOut = COALESCE((
    SELECT MAX(dtOut)
    FROM tbl as t1
    WHERE t1.type = tbl.type
    AND t1.dtIn < tbl.dtOut 
AND t1.dtOut > tbl.dtIn
    ), dtOut);

-- Do the actual updates of dtOut
UPDATE tbl
SET dtOut = COALESCE((
    SELECT MIN(dtIn)
    FROM tbl as t2
    WHERE t2.type = tbl.type AND
          t2.id <> tbl.id AND
          t2.dtIn >= tbl.dtIn AND t2.dtIn < tbl.dtOut
    ), dtOut);

--COMMIT TRANSACTION;
  • 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-08T03:17:11+00:00Added an answer on June 8, 2026 at 3:17 am

    Just off the top of my head I believe that one of Joe Celko’s books had this as an example problem. You might find the excerpt available on Google.

    This might be closer. I think you weren’t really doing the subquery the right way.

    UPDATE table
    SET dtOut = (
        SELECT MIN(t2.dtIn)
        FROM [table] as t2
        WHERE t2.id <> table.id AND t2.type = table.type
            AND table.dtIn < t2.dtIn AND t2.dtIn < table.dtOut
            AND table.dtOut <= t2.dtOut
        )
    WHERE EXISTS (
        SELECT 1
        FROM [table] as t3
        WHERE
                t3.type = table.type
            AND t3.id <> table.id
            AND table.dtIn < t3.dtIn AND t3.dtIn < table.dtOut
            AND table.dtOut <= t3.dtOut
        )
    

    EDIT
    I overlooked the id column at the top of the page so obviously that’s a better check than making sure the endpoints don’t match up. The solution is probably easier if you can assume that no two rows of identical type have the dtIn.

    Btw, there’s no reason to use a CROSS APPLY when a subquery will do exactly the same job.

    EDIT 2
    I did some quick testing and I think my query handles the scenario in your diagram. There’s one case where it might not do what you want.

    For a given type, think about the final two segments S1 and S2 in order of start time. S2 starts after S1 but also imagine it ends before S1 does. S2 is fully contained in the interval of S1, so it’s either ignorable or the information for the two segments needs to be split into a third segment and that’s where the problem gets trickier.

    So this solution just assumes they can be ignored.


    EDIT 3 based on the comment about combining updates

    SQLFiddle posted by OP

    -- eliminate redundant rows
    DELETE dT1 /* FROM tbl dT1 -- unnecessary */
    WHERE EXISTS
    (
        SELECT *
        FROM tbl dT2
        WHERE dT1.Type = dT2.Type AND dT1.dtIn = dT2.dtIn
        AND (
            dT1.dtOut < dT2.dtOut
            OR (dT1.dtOut = dT2.dtOut AND dT1.id < dT2.id)
        )
    );
    
    --adjust dtOuts to the max dates
    UPDATE tbl
    SET dtOut = COALESCE((
        SELECT MAX(dtOut)
        FROM tbl as t1
        WHERE t1.type = tbl.type
        ), dtOut);
    
    -- Do the actual updates of dtOut
    UPDATE tbl
    SET dtOut = COALESCE((
        SELECT MIN(dtIn)
        FROM tbl as t2
        WHERE t2.type = tbl.type AND
              t2.id <> tbl.id AND
              t2.dtIn >= tbl.dtIn AND t2.dtIn < tbl.dtOut
        ), dtOut);
    

    Either one of the two updates below should replace the two updates above.

    UPDATE tbl
    SET dtOut = (
        SELECT
            COALESCE(
                MIN(dtIn),
                /* as long as there's no GROUP BY, there's always one row */
                (SELECT MAX(dtOut) FROM tbl as tmax WHERE tmax.type = tbl.type)
            )
        FROM tbl as tmin
        WHERE tmin.type = tbl.type
    
            AND tmin.dtIn > tbl.dtIn
            /*  
            regarding the original condition in the second update:
                t2.dtIn >= tbl.dtIn AND t2.dtIn < tbl.dtOut
    
            dtIns can't be equal because you already deleted those
            and if dtIn was guaranteed to be less than dtOut it's
            also automatically always less than max(dtOut)
            */
    );
    
    UPDATE tbl
    SET dtOut = COALESCE(
      (
        SELECT MIN(dtIn) FROM tbl as tmin
        WHERE tmin.type = tbl.type AND tmin.dtIn > tbl.dtIn
      ),
      (        
        SELECT MAX(dtOut) FROM tbl as tmax
        WHERE tmax.type = tbl.type
      )
    );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was wondering if someone could help me with this. I have defined my
I was wondering if someone could help with this: I have a series of
I have an SQL problem i was wondering if someone could help me out.
Wondering if someone could help me. I have next to no knowledge with Ajax,
Just wondering if someone could help me with a very simple SQL query. I
was wondering if someone could help with this one. This is the first time
I was wondering if someone could help me understand this problem. I prepared a
I was wondering if someone could help me to get this method converted to
I was wondering if someone could help me get pointers to solve this problem.
I was wondering if someone could help me with the performance of this code

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.