Solved

SQL Join Question

Posted on 2011-03-22
4
251 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:
Ephraim Wangoya 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:Ephraim Wangoya
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

Suggested Solutions

Title # Comments Views Activity
Getting local user timezone in Sql Server 5 30
SqlServer Table Triggers 3 28
selective rebuild of SQL Tables in scheduled job 10 37
Replication failure 1 19
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
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.
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

713 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