Solved

SQL Join Question

Posted on 2011-03-22
4
245 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
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.
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.

856 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