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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T04:58:40+00:00 2026-06-06T04:58:40+00:00

I made a mistake in a bulk insert script, so now i have duplicate

  • 0

I made a mistake in a bulk insert script, so now i have “duplicate” rows with different colX. I need to delete this duplicate rows, but I cant figure out how. To be more precise, I have this:

 col1 | col2 | col3 | colX      
----+----------------------
  0   |  1   |  2   |  a
  0   |  1   |  2   |  b
  0   |  1   |  2   |  c
  0   |  1   |  2   |  a
  3   |  4   |  5   |  x
  3   |  4   |  5   |  y
  3   |  4   |  5   |  x
  3   |  4   |  5   |  z

and I want to keep the first occurrence of each (row, colX):

 col1 | col2 | col3 | colX      
----+----------------------
  0   |  1   |  2   |  a
  3   |  4   |  5   |  x

Thank you for your replies 🙂

  • 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-06T04:58:41+00:00Added an answer on June 6, 2026 at 4:58 am

    Try the simplest approach with Sql Server’s CTE: http://www.sqlfiddle.com/#!3/2d386/2

    Data:

    CREATE TABLE tbl
        ([col1] int, [col2] int, [col3] int, [colX] varchar(1));
    
    INSERT INTO tbl
        ([col1], [col2], [col3], [colX])
    VALUES
        (0, 1, 2, 'a'),
        (0, 1, 2, 'b'),
        (0, 1, 2, 'c'),
        (0, 1, 2, 'a'),
        (3, 4, 5, 'x'),
        (3, 4, 5, 'y'),
        (3, 4, 5, 'x'),
        (3, 4, 5, 'z');
    

    Solution:

    select * from tbl;
    
    with a as
    (
      select row_number() over(partition by col1 order by col2, col3, colX) as rn 
      from tbl   
    )
    delete from a where rn > 1;
    
    select * from tbl;
    

    Output:

    | COL1 | COL2 | COL3 | COLX |
    -----------------------------
    |    0 |    1 |    2 |    a |
    |    0 |    1 |    2 |    b |
    |    0 |    1 |    2 |    c |
    |    0 |    1 |    2 |    a |
    |    3 |    4 |    5 |    x |
    |    3 |    4 |    5 |    y |
    |    3 |    4 |    5 |    x |
    |    3 |    4 |    5 |    z |
    
    
    | COL1 | COL2 | COL3 | COLX |
    -----------------------------
    |    0 |    1 |    2 |    a |
    |    3 |    4 |    5 |    x |
    

    Or perhaps this: http://www.sqlfiddle.com/#!3/af826/1

    Data:

    CREATE TABLE tbl
        ([col1] int, [col2] int, [col3] int, [colX] varchar(1));
    
    INSERT INTO tbl
        ([col1], [col2], [col3], [colX])
    VALUES
        (0, 1, 2, 'a'),
        (0, 1, 2, 'b'),
        (0, 1, 2, 'c'),
        (0, 1, 2, 'a'),
        (0, 1, 3, 'a'),
        (3, 4, 5, 'x'),
        (3, 4, 5, 'y'),
        (3, 4, 5, 'x'),
        (3, 4, 5, 'z');
    

    Solution:

    select * from tbl;
    
    
    with a as
    (
        select row_number() over(partition by col1, col2, col3 order by colX) as rn 
        from tbl   
    )
    delete from a where rn > 1;
    
    select * from tbl;
    

    Output:

    | COL1 | COL2 | COL3 | COLX |
    -----------------------------
    |    0 |    1 |    2 |    a |
    |    0 |    1 |    2 |    b |
    |    0 |    1 |    2 |    c |
    |    0 |    1 |    2 |    a |
    |    0 |    1 |    3 |    a |
    |    3 |    4 |    5 |    x |
    |    3 |    4 |    5 |    y |
    |    3 |    4 |    5 |    x |
    |    3 |    4 |    5 |    z |
    
    | COL1 | COL2 | COL3 | COLX |
    -----------------------------
    |    0 |    1 |    2 |    a |
    |    0 |    1 |    3 |    a |
    |    3 |    4 |    5 |    x |
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have made a mistake in one of the commits. Now I want to
I have made this mistake several times, and found myself referencing This Post every
I made a mistake in the model declaration of an app and now need
I made the mistake of being born in New Zealand and now have to
I've made mistake and allowed two different routes pointing at same place. Now I've
Please help me finding where I have made mistake? delimiter // CREATE FUNCTION `count_photos_in_gallery`(`tmp_gallery_id`
UPDATE: I made a mistake in my debugging - this question is not relavent
Hi all I have problem with my QML code. I made mistake and I
Mysql mistake (don't laught please) I made a script that generates statistics table for
I have made the mistake when starting the coding an iPhone App of not

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.