Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SQL query to retrieve changes

Posted on 2007-03-26
5
Medium Priority
?
242 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 200 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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

705 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