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

  • Home
  • SEARCH
  • 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 8213477
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T11:07:55+00:00 2026-06-07T11:07:55+00:00

I have been working on a query to search and delete duplicate column values.

  • 0

I have been working on a query to search and delete duplicate column values. Currently I have this query (returns duplicates):

SELECT NUIP, FECHA_REGISTRO
FROM registros_civiles_nacimiento
WHERE NUIP IN (
 SELECT NUIP
 FROM registros_civiles_nacimiento
 GROUP BY NUIP
 HAVING (COUNT(NUIP) > 1)
) order by NUIP

This work returning a table like this:

NUIP        FECHA_REGISTRO
38120100138 1975-05-30
38120100138 1977-08-31
40051800275 1980-09-24
40051800275 1999-11-29
42110700118 1972-10-26
42110700118 1982-04-22
44030700535 1982-10-19
44030700535 1993-05-05
46072300777 1991-01-17
46072300777 1979-03-30

The thing is that I need to delete the rows with duplicate column values. But I need to delete the row with the oldest date, for example, for the given result, once the needed query is performed, this is the list of result that must be kept:

NUIP        FECHA_REGISTRO
38120100138 1977-08-31
40051800275 1999-11-29
42110700118 1982-04-22
44030700535 1993-05-05
46072300777 1991-01-17

How can I do this using plain SQL?

  • 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-07T11:07:57+00:00Added an answer on June 7, 2026 at 11:07 am
    --PULL YOUR SELECT OF RECS WITH DUPES INTO A TEMP TABLE 
    --(OR CREATE A NEW TABLE SO THAT YOU CAN KEEP THEM AROUND FOR LATER IN CASE)
    SELECT   NUIP,FECHA_REGISTRO
    INTO #NUIP 
    FROM     SO_NUIP
    WHERE NUIP IN (
    SELECT NUIP
     FROM SO_NUIP
     GROUP BY NUIP
     HAVING (COUNT(NUIP) > 1)
    )
    
    --CREATE FLAG FOR DETERMINIG DUPES
    ALTER TABLE #NUIP ADD DUPLICATETOREMOVE bit
    
     --USE `RANK()` TO SET FLAG
     UPDATE #NUIP
     SET DUPLICATETOREMOVE = CASE X.RANK
            WHEN  1 THEN 1
            ELSE 0 
            END
    --SELECT *
    FROM #NUIP A
    INNER JOIN (SELECT NUIP,FECHA_REGISTRO,RANK() OVER (PARTITION BY [NUIP] ORDER BY    FECHA_REGISTRO ASC) AS RANK
    FROM #NUIP) X ON X.NUIP = A.NUIP AND X.FECHA_REGISTRO = A.FECHA_REGISTRO
    
    --HERE IS YOUR DELETE LIST
    SELECT * 
    FROM so_registros_civiles_nacimiento R
    JOIN #NUIP N ON N.NUIP = R.NUIP AND N.FECHA_REGISTRO = R.FECHA_REGISTRO
    WHERE N.DUPLICATETOREMOVE = 1
    
    --HERE IS YOUR KEEP LIST
    SELECT * 
    FROM so_registros_civiles_nacimiento R
    JOIN #NUIP N ON N.NUIP = R.NUIP AND N.FECHA_REGISTRO = R.FECHA_REGISTRO
    WHERE N.DUPLICATETOREMOVE = 0
    
    --ZAP THEM AND COMMIT YOUR TRANSACTION, YOU'VE STILL GOT A REC OF THE DELETEDS FOR AS LONG AS THE SCOPE OF YOUR #NUIP
    BEGIN TRAN --COMMIT  --ROLLBACK
    DELETE FROM so_registros_civiles_nacimiento
    JOIN #NUIP N ON N.NUIP = R.NUIP AND N.FECHA_REGISTRO = R.FECHA_REGISTRO
    WHERE N.DUPLICATETOREMOVE = 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have been working on query which uses compute by clause for avg and
I am having trouble writing a query. I have been working with UNION on
Have been working on this question for a couple hours and have come close
I have been working on keeping things object oriented for my project. Currently, I'm
I have been working on this problem for 2 days now and it's an
I have a tableview with a search display controller. It has been working fine
I have been working on a query that will return a suggested start date
Hi I have been working on this code today after attempting some of the
I have been working on speeding up a query I'm using for about a
I have been working on making a Search using Solrnet which is working 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.