Solved

SQL query to retrieve changes

Posted on 2007-03-26
5
238 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 143

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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
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.

632 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