Solved

SQL query to retrieve changes

Posted on 2007-03-26
5
208 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
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
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

706 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

18 Experts available now in Live!

Get 1:1 Help Now