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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:27:01+00:00 2026-06-04T13:27:01+00:00

I have the following rows: CREATE TABLE #TEMP (id int, name varchar(255), startdate datetime,

  • 0

I have the following rows:

CREATE TABLE #TEMP (id int, name varchar(255), startdate datetime, enddate datetime)
INSERT INTO #TEMP VALUES(1, 'John', '2011-01-11 00:00:00.000','2011-01-11 00:01:10.000')
INSERT INTO #TEMP VALUES(2, 'John', '2011-01-11 00:00:20.000','2011-01-11 00:01:05.000')
INSERT INTO #TEMP VALUES(3, 'John', '2011-01-11 00:01:40.000','2011-01-11 00:01:50.000')
INSERT INTO #TEMP VALUES(4, 'Adam', '2011-01-11 00:00:40.000','2011-01-11 00:01:20.000')
INSERT INTO #TEMP VALUES(5, 'Adam', '2011-01-11 00:00:45.000','2011-01-11 00:01:15.000')

SELECT * FROM #TEMP

DROP TABLE #TEMP

I am trying to remove records that have dates contained within other dates to obtain the following:

John 2011-01-11 00:00:00.000 2011-01-11 00:01:10.000
John 2011-01-11 00:01:40.000 2011-01-11 00:01:50.000
Adam 2011-01-11 00:00:40.000 2011-01-11 00:01:20.000

Any suggestions on how to achieve this for a table of about 100K rows?

  • 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-04T13:27:02+00:00Added an answer on June 4, 2026 at 1:27 pm

    This gives the desired result:

    DELETE T1 FROM #TEMP T1
    WHERE EXISTS(
        SELECT NULL FROM #TEMP T2
        WHERE   t1.id <> t2.id
        AND     t1.name = t2.name
        AND     t1.startdate >= t1.startdate
        AND     t1.enddate   <= t1.enddate
    )
    

    http://msdn.microsoft.com/en-us/library/ms188336.aspx

    Edit: I’ve just noticed that there’s one problem. If there are duplicates (same start- and enddate), both would be deleted (none with John’s approach, even with only one equal date). So you need to take this into account:

    DELETE T1 FROM #TEMP T1
    WHERE EXISTS(
        SELECT NULL FROM #TEMP T2
        WHERE   t1.id <> t2.id
        AND     t1.name = t2.name
        AND     t1.startdate > t2.startdate
        AND     t1.enddate   < t2.enddate
        OR      t1.id <> t2.id
        AND     t1.name = t2.name
        AND     t1.startdate = t2.startdate
        AND     t1.enddate   < t2.enddate
        OR      t1.id <> t2.id
        AND     t1.name = t2.name
        AND     t1.startdate > t2.startdate
        AND     t1.enddate   = t2.enddate
        OR      t1.id > t2.id
        AND     t1.name = t2.name
        AND     t1.startdate = t2.startdate
        AND     t1.enddate   = t2.enddate
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table as: CREATE TABLE tbl_temp (id serial, friend_id int, name varchar(32));
I have the following table schema: CREATE TABLE [Foo]( [id] [int] NOT NULL, [name]
I have a the following table with rows: =============================================== id| name | group1 |
I Have the following table: CREATE TABLE `tmp_table` ( `id` int(11) NOT NULL AUTO_INCREMENT,
I have created table #temp with columns id as int identity(1,1) and name as
I have the following table CREATE TABLE [dbo].[LogFiles_Warehouse]( [id] [int] IDENTITY(1,1) NOT NULL, [timestamp]
I have the following table: CREATE TABLE IF NOT EXISTS `notes` ( `id` int(10)
I have the following table structures CREATE TABLE [dbo].[WorkItem]( [WorkItemId] [int] IDENTITY(1,1) NOT NULL,
I have the following: CREATE TABLE food_delivery ( foodtype varchar(50) NOT NULL, foodname varchar(50)
I have the following example code: create table #tempmembers ( memberid int ) update

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.