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?
-
1Add some sample table data as well as the expected result. Formatted text please, not images,.jarlh– jarlh2017-12-12 14:30:58 +00:00Commented Dec 12, 2017 at 14:30
-
and also db platformOldProgrammer– OldProgrammer2017-12-12 14:31:12 +00:00Commented 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.Gholamali Irani– Gholamali Irani2017-12-12 14:38:41 +00:00Commented Dec 12, 2017 at 14:38
Add a comment
|
2 Answers
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
1 Comment
Abhijit
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
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.