SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Compare column rows in two tables


Compare column rows in two tables

Author
Message
sanspirit
sanspirit
Grasshopper
Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)

Group: General Forum Members
Points: 16 Visits: 21
Hi

I would like to compare rows which are in two different tables which have the same column and data type where the data in one table1 column (A) has repeated data. When new data is added to table1 column (A) the unique rows should be selected and added to another table2 column (A) on the same database.

Both tables have the same column name but the rows in table1 column (A) are repeated while the rows in table2 column (A) are unique.

Data is always added to table1 columnA everyday. How do i selected only the unique rows in table1 column (A) and add it to table 2 (A). To give an idea there are about 50,000 rows in table1 column (A) and about 1000 rows in table 2 column(A)

Any help is much appreciated. New to SQL so please give in detail.

Thanks.
Samuel Vella
Samuel Vella
SSC Eights!
SSC Eights! (813 reputation)SSC Eights! (813 reputation)SSC Eights! (813 reputation)SSC Eights! (813 reputation)SSC Eights! (813 reputation)SSC Eights! (813 reputation)SSC Eights! (813 reputation)SSC Eights! (813 reputation)

Group: General Forum Members
Points: 813 Visits: 2144
use a distinct on the select from table A:

Select Distinct col1, col2, col3
from tableA



add a clause if you want to exclude everytihng from tableB

Select Distinct a.col1, a.col2, a.col3
from TableA a
left outer Join TableB b
on a.col1 = b.col1
where b.col1 is null


John Mitchell-245523
John Mitchell-245523
SSChampion
SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)

Group: General Forum Members
Points: 13982 Visits: 15944
Please will you post your table structures in the form of CREATE TABLE statements and also give some sample data in the form of INSERT statements. Also, please show what you would expect to happen when further sample data is added. It's difficult to know what you mean by duplicate data - is it where the whole row is the same, or just the primary key?

John
Samuel Vella
Samuel Vella
SSC Eights!
SSC Eights! (813 reputation)SSC Eights! (813 reputation)SSC Eights! (813 reputation)SSC Eights! (813 reputation)SSC Eights! (813 reputation)SSC Eights! (813 reputation)SSC Eights! (813 reputation)SSC Eights! (813 reputation)

Group: General Forum Members
Points: 813 Visits: 2144
here's a good article on how to post:
http://www.sqlservercentral.com/articles/Best+Practices/61537/

as you're new to SQL, have a look at my first reply if any confuses you then you need to get an SQL book and look those terms up
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search