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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T08:49:28+00:00 2026-06-08T08:49:28+00:00

Say, I have a table like this (SQL Server 2008): CREATE TABLE tbl (ID

  • 0

Say, I have a table like this (SQL Server 2008):

CREATE TABLE tbl (ID INT, dtIn DATETIME2, dtOut DATETIME2, Type INT)

INSERT tbl VALUES
(1, '05:00', '6:00', 1),  -- will be removed
(2, '05:00', '7:00', 1),  -- will be removed
(3, '05:01', '8:00', 1),
(4, '05:00', '8:00', 1),
(5, '05:00', '6:00', 2),  -- will be removed
(6, '05:00', '7:00', 2),
(7, '05:00', '7:00', 3),
(8, '04:00', '7:00', 3)

I need to remove all records of the same ‘type’ (if 2 or more are found) with the same ‘dtIn’ for their ‘type’, except the one with the largest ‘dtOut’. In other words, the table above should result in this:

(3, '05:01', '8:00', 1),   -- no matching 'dtIn' for 'type' = 1
(4, '05:00', '8:00', 1),   -- largest 'dtOut' for 'type' = 1
(6, '05:00', '7:00', 2),   -- largest 'dtOut' for 'type' = 2
(7, '05:00', '7:00', 3),   -- no matching 'dtIn' for 'type' = 3
(8, '04:00', '7:00', 3)    -- no matching 'dtIn' for 'type' = 4

How do you even select several rows with the equal type from the same table. You can’t do select * from tbl where type=type…. Anyway, I’d appreciate some help with this…

  • 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-08T08:49:29+00:00Added an answer on June 8, 2026 at 8:49 am

    Here’s one way to select the rows you want to delete:

    SELECT *
    FROM tbl T1
    WHERE EXISTS
    (
        SELECT *
        FROM tbl T2
        WHERE T1.Type = T2.Type
        AND T1.dtIn = T2.dtIn
        AND (
                T1.dtOut < T2.dtOut
                OR (T1.dtOut = T2.dtOut AND T1.id < T2.id)
            )
    )
    

    This query can also be easily changed to actually delete the rows. Just change SELECT * to DELETE T1. But please do test that it does what you want before actually running the delete statement.

    See it working online: sqlfiddle


    Update

    Here’s an approach using ROW_NUMBER:

    ;WITH T1 AS (
        SELECT id, ROW_NUMBER() OVER (PARTITION BY Type, dtIn
                                      ORDER BY dtOut DESC, ID DESC) AS rn
        FROM tbl
    )
    SELECT * FROM tbl
    WHERE id IN
    (
        SELECT id
        FROM T1
        WHERE rn > 1
    )
    

    See it working online: sqlfiddle

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

Sidebar

Related Questions

i have a table with jobs like this in a MS SQL Server 2008
Let us say that I have a table structured like this(using SQL server): empID
I'm using T-Sql with SQL Server 2008. Say I have parent table: Projects: ProjectID
Let's say I have some data, either in a SQL Server 2008 table or
(Note: this is for MS SQL Server) Say you have a table ABC with
To keep things very simple, say I have a SQL Server 2008 table with
I have a simple SQL Server 2008 database with two tables like this: TableA:
Let's say I have a table like this: CREATE TABLE t( [guid] [uniqueidentifier] NOT
Assuming I have a table like this one: CREATE TABLE user_delegates ( [id] INT
We have a SQL server 2008 and one of the tables, say table A

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.