Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Compare column rows in two tables Expand / Collapse
Author
Message
Posted Wednesday, January 9, 2008 4:38 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, July 8, 2009 9:33 AM
Points: 12, 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.
Post #440533
Posted Wednesday, January 9, 2008 5:21 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Wednesday, August 27, 2014 9:56 AM
Points: 337, Visits: 1,988
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

Post #440548
Posted Wednesday, January 9, 2008 5:21 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 10:00 AM
Points: 5,330, Visits: 9,777
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
Post #440549
Posted Wednesday, January 9, 2008 6:57 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Wednesday, August 27, 2014 9:56 AM
Points: 337, Visits: 1,988
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
Post #440603
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse