Solved

SQL Join Question

Posted on 2011-03-22
4
239 Views
Last Modified: 2012-05-11
Hi, I'm new to T-SQL joins and was wondering how one would build a SQL query that compares two tables and identifies changes between the two.

Example:  I have listed two examples tables below.  The SQL query should compare the records between each table (by ID) looking for changes in color between the two entries.  The SQL query should return the the values for the CarID, CarColor, and CarNewColor for the rows with an ID of "1" only because this entry's color changed from red to orange (is different between the two tables).  In the example below, the entries with ID of 2 and 3 did not change color.

Thank you in advance.


Car
CarID   CarColor
1           red
2           red
3           blue

Car_new
CarNewID  CarNewColor
1                 orange
2                 red
3                 blue
0
Comment
Question by:karlhsc
  • 2
4 Comments
 
LVL 32

Accepted Solution

by:
ewangoya earned 250 total points
ID: 35191753

select *
from car
inner join Car_new on car_new.id = car.ID and Car_new.CarNewColor <> Car.Car.CarColor
0
 
LVL 18

Assisted Solution

by:Cluskitt
Cluskitt earned 250 total points
ID: 35191762
SELECT Car.CarID, Car.CarColor, Car_new.CarNewColor
FROM Car INNER JOIN Car_New
ON Car.CarID=Car_new.CarNewID AND Car.CarColor<>Car_new.CarNewColor
0
 
LVL 32

Expert Comment

by:ewangoya
ID: 35191763

select *
from car
inner join Car_new on car_new.carnewid = car.carID and Car_new.CarNewColor <> Car.CarColor
0
 
LVL 15

Expert Comment

by:Aaron Shilo
ID: 35191792
select * from (
select CarID,
case when car.CarColor<>new_car.carcolor then new_car.carcolor else 'X' end carcolor
from car join car_new
on car.id=car_new.id)
where carcolor <> 'X'
0

Featured Post

Microsoft Certification Exam 74-409

VeeamĀ® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
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.

777 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