Solved

SQL query to retrieve changes

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

808 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