6

I have created this statement in access 2003

  SELECT COUNT(*)
  FROM TABLEA
  WHERE NOT EXISTS(SELECT * FROM TABLEB);

Does this statement helps to check if the records in table A is the same as table b? TABLEA is the new table of table b and i want to make sure that all records from table b are in table A.

Secondly i have this table TABLEC. How can i check if there are duplicate records, meaning all the fields values are the same, in TABLEC?

10e5x
  • 909
  • 4
  • 15
  • 27

2 Answers2

5

The answer is: No, your query does not make sense.

To tell whether two records are 'same', you have to define the term 'equal'. Should all fields be equal? Or only certain fields?

If you have two Tables TableA and TableB and they have two fields 'A' and 'B', then this statement finds all records which exist in both tables:

select distinct TableA.* 
  from TableA 
       join TableB 
         on TableA.A = TableB.A 
        and TableA.B = TableB.B

Or

select * 
  from TableA
 where exists (
     select 1 
       From TableB 
      where TableA.A = TableB.A 
        and TableA.B = TableB.B
     )

Edit: User 10e5x pointed out that his table contains NULL values. So the comparison per field has to be a bit more complicated to compensate the NULL comparison caveats.

I will just give the WHERE part:

where TableA.A = TableB.A or coalesce (TableA.A, TableB.A) is NULL 
  and TableA.B = TableB.B or coalesce (TableA.B, TableB.B) is NULL

The function coalesce(a,b,c...) returns the leftmost non NULL value, hence

coalesce (A,B) is NULL
-- is equal to
A is NULL and B is NULL

Note: This tricky coding is a reason why you should avoid NULL values in columns which are used for comparison.

PiotrWolkowski
  • 8,408
  • 6
  • 48
  • 68
alzaimar
  • 4,572
  • 1
  • 16
  • 30
  • Ya all fields have to be equal to be consider duplicate. If i have 45 columns i need to do the where clause with 45 comparison? – 10e5x Jan 28 '13 at 08:04
  • 2
    Yes. You have to test each of the 45 fields. – alzaimar Jan 28 '13 at 18:50
  • hi wonder if u are there. I tried your method. It return me none of the records are the same. Then i do a check, most of the records are totally the same. I have two columns which are null, is it becuz of that? – 10e5x Feb 01 '13 at 03:59
  • Comparing NULL values is tricky, because NULL does not compare to anything, not even to NULL. I will edit my proposal to compensate NULL values. – alzaimar Feb 01 '13 at 07:58
-3

Try this

SELECT documentno FROM TableA INTERSECT SELECT documentno FROM TableB