Subject Re: [firebird-support] How to compare two tables?
Author Helen Borrie
At 07:44 PM 15/07/2003 +0000, you wrote:
>Hi, is there a "clever" sql way to compare two tables?
>Put that table A and table B have this structure:
>EMPLOYER_ID, SALARY
>I want to find:
>a) if A has EMPLOYERS (records) that B misses
>b) if B has EMPLOYERS (records) that A misses
>c) if an employer in A table has a different salary than the same has
>in B table (an vice versa, but seems not necessary to check ;))

Will this do it?

SELECT
A.EMPLOYER_ID, B.EMPLOYER_ID, A.SALARY, B.SALARY
FROM A
FULL JOIN B
ON A.EMPLOYER_ID = B.EMPLOYER_ID
GROUP BY A.EMPLOYER_ID, B.EMPLOYER_ID, A.SALARY, B.SALARY
HAVING
((A.EMPLOYER_ID IS NULL)
OR (B.EMPLOYER_ID IS NULL))
OR
((A.EMPLOYER_ID = B.EMPLOYER_ID)
AND
(
(A.SALARY IS NULL AND B.SALARY IS NOT NULL)
OR (A.SALARY IS NOT NULL AND B.SALARY IS NULL)
OR (A.SALARY IS NULL AND B.SALARY IS NULL) /* if you want this */
OR (A.SALARY <> B.SALARY)
)
)

heLen