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 |
kneekill
Yak Posting Veteran
76 Posts |
Posted - 2011-08-04 : 08:27:44
|
Hi AllI have to come up with a select query for the following scenario.There are 2 tables "TempMismatch" and "TempData"."TempMismatch" contains code and its mismatching code. 1 code can have multiple mismatching codes.CREATE TABLE #TempMismatch ( Original varchar(10),Mismatch varchar(10))INSERT INTO #TempMismatch (Original,Mismatch)SELECT 'AA','BB'INSERT INTO #TempMismatch (Original,Mismatch)SELECT 'XX','YY'INSERT INTO #TempMismatch (Original,Mismatch)SELECT 'CC','DD'INSERT INTO #TempMismatch (Original,Mismatch)SELECT 'EE','FF'INSERT INTO #TempMismatch (Original,Mismatch)SELECT 'AA','XX'"TempData" contains just the codes in a single column.CREATE TABLE #TempData ( Code varchar(10))INSERT INTO #TempData (Code)SELECT 'AA'INSERT INTO #TempData (Code)SELECT 'BB'INSERT INTO #TempData (Code)SELECT 'CC'INSERT INTO #TempData (Code)SELECT 'XX'INSERT INTO #TempData (Code)SELECT 'YY'Select * from #TempMismatchSelect * from #TempData--Drop table #TempMismatch--Drop table #TempDataNow for every “code” in #TempData I need to select the corresponding row from #TempMismatch where “code” equals “Original” And the “Original”’s “mismatch” is present as one of the rows in #TempData.So in the above example. The result would be.AA,BBAA,XXXX,YYHigly appriciate your help with respect to building the best possible query for the problem. |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-08-04 : 08:47:42
|
isnt this a matter of simple join between tables on condition #TempData.Code=#TempMismatch.Original------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
kneekill
Yak Posting Veteran
76 Posts |
Posted - 2011-08-04 : 08:57:00
|
quote: Originally posted by visakh16 isnt this a matter of simple join between tables on condition #TempData.Code=#TempMismatch.Original------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
Hi visakhthe normal condition would also returnCC,DDAs DD is not in the "TempData" table I do not want it to be returned.Inner join would returnAA BBAA XXCC DDXX YYright join would returnNULL NULLAA BBAA XXCC DDXX YYand left join would returnAA BBAA XXCC DDEE FFXX YY |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-08-04 : 09:02:42
|
nope. this should workSELECT tm.Original,tm.MismatchFROM #TempData td1JOIN #TempMismatch tmON tm.Original = td1.CodeJOIN #TempData td2ON td2.Code = tm.MismatchORDER BY tm.Original,tm.Mismatch ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
kneekill
Yak Posting Veteran
76 Posts |
Posted - 2011-08-04 : 09:25:44
|
Thanks a lot Visakh |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-08-04 : 09:33:18
|
wc------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|