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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:26:11+00:00 2026-06-09T15:26:11+00:00

First I would like to remark that I (as a Newb) did search through

  • 0

First I would like to remark that I (as a Newb) did search through several Q & A regarding duplicates in a table though unfortunately for me, I couldn’t manipulate the code being used as answer.

My table is made out of a report being sorted in SQL Server 2008.

I would like to know how do I remove duplicate records and with an explanation.

"MyTable":

Column1   (PK-auto incremental table's record ID) 
Column2   (some TXT) 
Column3   (Some TXT)
Column4   (SmallDateTime)
Column5   is empty 

Column5 will hold the value of SUM(count of deleted duplicates including this survived row)

The key to the solution in may case is if [column2 and column3] have multiple records with same content (hence Duplicates) only they don’t always share the same date (column4).

From this:

col1  col2   col3  col4         col5
----  -----  ----  -----------  ----
1     [abc]  [4]   [10/1/2012]  null
2     [abc]  [1]   [12/1/2012]  null
3     [ghi]  [6]   [4/1/2012]   null
4     [def]  [5]   [8/1/2012]   null
5     [abc]  [4]   [10/1/2012]  null
6     [def]  [5]   [12/1/2012]  null
7     [ghi]  [6]   [15/1/2012]  null
8     [abc]  [4]   [17/1/2012]  null
9     [ghi]  [6]   [6/1/2012]   null
10    [abc]  [1]   [13/1/2012]  null

Into this:

col1  col2   col3  col4         col5
----  -----  ----  -----------  ----
8     [abc]  [4]   [17/1/2012]  2
10    [abc]  [1]   [13/1/2012]  3
6     [def]  [5]   [12/1/2012]  2
7     [ghi]  [6]   [15/1/2012]  3

Meaning leave the latest (1) as a representation of every duplicated record.

++ReEditing++

Aaron Bertrand
shawnt00
e2nburner… and rest of u all
i can’t say how much i thank your Reply although i did not yet comprehend that mass of code.
i am now going to check those codes but not b4 thanking you guys !!

when i first started to program and needed sql querys, after using

Select * From MyTable

… my 1’st SQL Statement …

i said HEY i know SQL !!! …. Now … look at that deep knowledge of you guys … THANKS A LOT i know that this post in StackOverFlow will be further useful for other beginners too

  • 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-09T15:26:13+00:00Added an answer on June 9, 2026 at 3:26 pm

    This answer uses a common table expression to apply row_number() and count() to each “slice” of data (meaning grouped by col2 + col3). The count() is used to identify how many rows belong to each such group, and the row_number() is used to apply a “rank” ordered by col4 desc (1 = latest per group, 2 = 2nd latest, etc). This also uses col1 (which looks like a unique column) to break any ties. The CTE can be followed by a query such as a select, update, delete, etc. So you can run the first select to validate that these are the rows you want to keep, and that the counts are correct. If they are, then you can proceed with the updates and deletes. You’ll notice that in all cases the row_number() output is used to identify the rows you keep or the rows you discard.

    To identify the rows you want to keep:

    ;WITH n AS 
    (
      SELECT col1, col2, col3, col4, 
        c = COUNT(*) OVER (PARTITION BY col2, col3),
        rn = ROW_NUMBER() OVER 
        (
          PARTITION BY col2, col3 ORDER BY col4 DESC, col1 DESC
        )
      FROM dbo.table_name
    )
    SELECT col1, col2, col3, col4, c
      FROM n WHERE rn = 1;
    

    Once you’ve confirmed that those are the row you want to keep, you can update them like this:

    ;WITH n AS 
    (
      SELECT col1, col2, col3, col4, col5, 
        c = COUNT(*) OVER (PARTITION BY col2, col3),
        rn = ROW_NUMBER() OVER 
        (
          PARTITION BY col2, col3 ORDER BY col4 DESC, col1 DESC
        )
      FROM dbo.table_name
    )
    UPDATE n SET col5 = c
      WHERE rn = 1;
    

    Then delete the remainders this way:

    ;WITH n AS 
    (
      SELECT col1, col2, col3, col4, 
        rn = ROW_NUMBER() OVER 
        (
          PARTITION BY col2, col3 ORDER BY col4 DESC, col1 DESC
        )
      FROM dbo.table_name
    )
    DELETE n WHERE rn > 1;
    

    Or even more simply (assuming col5 was completely null before the update):

    DELETE dbo.table_name WHERE col5 IS NULL;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First I would like to say that I am new to this site, never
I would first like to admit that I am an extremely novice developer, so
First off I would like to say that I think that my question may
First off I would like to say that this is my first post and
First I would like to say that I am entirely new to Magento so
First I would like to say that I have only worked with java for
At first I would like to point out, that I am not a JQuery
Good day, first I would like to tell that I am not proud to
First of all I would like to remark I am new with the concept
(Updated, please look at the EDIT, thanks!) First I would like to point that

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.