Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
AskSQLTeam
Ask SQLTeam Question
0 Posts |
Posted - 2004-02-18 : 08:03:20
|
Ammar writes "I have two tables with three common fields. I would like to know SQL Query to match the rows on (<>) basis on all three fields as complete row. Here is an example.Table 1.Price Size Year10 2 200310 3 200320 2 200320 3 2003Table 2.Price Size Year10 2 200320 3 2003I need Following Results10 3 200320 2 2003 ExplainationLogically it should subtract Table 2 From Table 1 and give me the remainder, and it should match complete row not column by column match. I have another solution of converting to a varchar string and then matching but that is not acceptable." |
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2004-02-18 : 08:36:54
|
select t1.*from tbl1 t1left outer join tbl2 t2on t1.Price = t2.Price and t1.Size = t2.Sizeand t1.Year = t2.Yearwhere t2.Price is null==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
|
|
AndrewMurphy
Master Smack Fu Yak Hacker
2916 Posts |
Posted - 2004-02-18 : 08:38:01
|
select * from table1 t1left join table2 t2 on t1.price=t2.price and t1.size=t2.size and t1.year=t2.yearwhere t2.price is nullunionselect * from table2 t1left join table2 t2 on t1.price=t2.price and t1.size=t2.size and t1.year=t2.yearwhere t2.price is nullshould get you there.... |
|
|
aw
Starting Member
1 Post |
Posted - 2004-02-18 : 10:30:10
|
quote: Originally posted by AndrewMurphy select * from table1 t1left join table2 t2 on t1.price=t2.price and t1.size=t2.size and t1.year=t2.yearwhere t2.price is nullunionselect * from table2 t1left join table2 table1 t2 on t1.price=t2.price and t1.size=t2.size and t1.year=t2.yearwhere t2.price is nullshould get you there....
slight typo fix -also, assuming the question is not (answer 1)select * from table1 minusselect * from table2but insteadselect * from (select * from table1 union all select * from table2)group by price, size, yearhaving count(*) = 1how about -select * from table1 t1full outer join table2 t2 on t1.price=t2.price and t1.size=t2.size and t1.year=t2.yearwhere t2.price is null |
|
|
|
|
|