0

I have a Table A from where I have to copy Data to Table B. Now problem is In both table A and Table B there is a column ID which is primary key and can't be null.Table A is having Duplicates. Can any one tell me How to insert Data into Table B from Table A without Duplicates?

3
  • 1
    Add some sample table data as well as the expected result. Formatted text please, not images,. Commented Dec 12, 2017 at 14:30
  • and also db platform Commented Dec 12, 2017 at 14:31
  • How (column ID which is primary key and can't be null.Table A is having Duplicates) ? Duplicate on ID? or other columns? ID is primary key. Commented Dec 12, 2017 at 14:38

2 Answers 2

3

It would be something like

INSERT INTO TableA(ID) SELECT DISTINCT ID FROM TableB B LEFT JOIN TableA A ON A.ID = B.ID WHERE A.ID IS NULL
Sign up to request clarification or add additional context in comments.

1 Comment

I can't use distinct over ID Column as ID is having Distinct Records. But other columns are having Duplicates. I was trying with Row_Number over partition By. But still problem not solving
0

You can use the DISTINCT function in a select statement to remove duplicates. In the example I'm going to assume that both tables have 3 columns called ID, Name and Surname:

insert into tableB (ID, Name, Surname)
select
    distinct(ID) as ID
    ,Name
    ,Surname
from tableA
;

Please note that the DISTINCT function will provide distinct rows.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.