Solved

SQL query to retrieve changes

Posted on 2007-03-26
5
218 Views
Last Modified: 2010-03-20
Hi…
Let's say I have 2 tables with id \ loginname

Table A

1 eladr
2 billc
3 tonyu

Table B

1 eladr
2 billca
3 tonyu
4 davidv

I need to write sql query to retrieve only records with changes comparing the tables.
In this case only records 2 and 4 are different (2 is different because of the loginame and 4 is new)

0
Comment
Question by:eladr
5 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 18791325
please try this:

select *
from tableB b
left join tableA a
  on a.id = b.id
  and a.name <> b.name
0
 
LVL 50

Accepted Solution

by:
Lowfatspread earned 50 total points
ID: 18791649
select  case when a.id is null then 'New b'
                   when b.id is null then 'New A'
                   else 'Change'
                   end as Type
          ,coalesce(a.id,b.id) as id
          ,a.loginname
          ,b.loginname
from tablea As a
full outer join tableb as b
 on a.id=b.id
Where (a.id is null
              or b.id is null )
       or (A.ID=B.ID AND NOT (A.NAME IS NULL AND B..NAME IS NULL) AND A.NAME <> B.NAME)
0
 
LVL 9

Expert Comment

by:TheSloath
ID: 18791669
You need to have a subquery that creates a unique list of all the ID's first, then LEFT JOIN that to the 2 tables and pick up the nulls or differences. E.G. with TableA and Table B, fields ID and Name:

Select TableIDList.ID, TableA.Name As NameA, TableB.Name As NameB
FROM ((SELECT ID FROM TableA
UNION
SELECT ID FROM TableB) As TableIDList LEFT JOIN TableA ON TableIDList.ID = TableA.ID) LEFT JOIN TableB ON TableIDList.ID = TableB.ID
WHERE TableA.Name <> TableB.Name OR TableA.Name IS NULL OR TableB.Name IS NULL
0
 
LVL 9

Expert Comment

by:TheSloath
ID: 18791734
Sorry - I thought this post came from the MS Access forum,which does not support the FULL OUTER JOIN in it's SQL. If the database system you are using supports the FULL OUTER JOIN that would be best...
0
 
LVL 1

Author Comment

by:eladr
ID: 18791893
10x
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Oracle DB monitor SW 21 47
SQL Help 27 40
TSQL - How to declare table name 26 28
Separate 2 comma delimited columns into separate rows 2 27
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now