Monday, June 30, 2008

Fast SQL Diff

Not my work and obsoleted by INTERSECT, EXCEPT / MINUS, but if your dbms doesn't have these commands then this is a fast way to do it:

SELECT MIN(TableName) as TableName, ID, COL1, COL2, COL3 ...
FROM
(
SELECT 'Table A' as TableName, ID, COL1, COL2, COL3, ...
FROM A
UNION ALL
SELECT 'Table B' as TableName, ID, COL1, COl2, COL3, ...
FROM B
) tmp
GROUP BY ID, COL1, COL2, COL3 ...
HAVING COUNT(*) = 1
ORDER BY ID

See the full article:

http://weblogs.sqlteam.com/jeffs/archive/2004/11/10/2737.aspx

No comments: