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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T06:38:13+00:00 2026-05-11T06:38:13+00:00

I need to transfer data from one table to another. The second table got

  • 0

I need to transfer data from one table to another. The second table got a primary key constraint (and the first one have no constraint). They have the same structure. What I want is to select all rows from table A and insert it in table B without the duplicate row (if a row is0 duplicate, I only want to take the first one I found)

Example :

MyField1 (PK)   |   MyField2 (PK)   |   MyField3(PK)   |   MyField4   |   MyField5    ----------  1               |   'Test'          |   'A1'           |   'Data1'    |   'Data1'   2               |   'Test1'         |   'A2'           |   'Data2'    |   'Data2'   2               |   'Test1'         |   'A2'           |   'Data3'    |   'Data3'   4               |   'Test2'         |   'A3'           |   'Data4'    |   'Data4' 

Like you can see, the second and third line got the same pk key, but different data in MyField4 and MyField5. So, in this example, I would like to have the first, second, and fourth row. Not the third one because it’s a duplication of the second (even if MyField4 and MyField5 contain different data).

How can I do that with one single select ?

thx

  • 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. 2026-05-11T06:38:14+00:00Added an answer on May 11, 2026 at 6:38 am

    First, you need to define what makes a row ‘first’. I’ll make up an arbitrary definition and you can change the SQL as you need to for what you want. For this example, I assume ‘first’ to be the lowest value for MyField4 and if they are equal then the lowest value for MyField5. It also accounts for the possibility of all 5 columns being identical.

    SELECT DISTINCT      T1.MyField1,      T1.MyField2,      T1.MyField3,      T1.MyField4,      T1.MyField5 FROM      MyTable T1 LEFT OUTER JOIN MyTable T2 ON      T2.MyField1 = T1.MyField1 AND      T2.MyField2 = T1.MyField2 AND      T2.MyField3 = T1.MyField3 AND      (           T2.MyField4 > T1.MyField4 OR           (                T2.MyField4 = T1.MyField4 AND                T2.MyField5 > T1.MyField5           )      ) WHERE      T2.MyField1 IS NULL 

    If you also want to account for PKs that are not duplicated in the source table, but already exist in your destination table then you’ll need to account for that too.

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

Sidebar

Ask A Question

Stats

  • Questions 179k
  • Answers 179k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Drupal will pass any additional URL elements as additional parameters… May 12, 2026 at 3:55 pm
  • Editorial Team
    Editorial Team added an answer IllegalArgumentException does have constructors that take a Throwable cause parameter… May 12, 2026 at 3:55 pm
  • Editorial Team
    Editorial Team added an answer This appears to work. There may well be a less… May 12, 2026 at 3:55 pm

Related Questions

As usual, some background information first: Database A (Access database) - Holds a table
The problem is during inserting data from staging table during import routine. The system
I have a data flow task that extracts data from one database and inserts
My main question is how to copy a whole database having a English collation

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.