| Author |
Topic |
|
asafg
Starting Member
39 Posts |
Posted - 2008-12-31 : 04:19:29
|
| I what to find the records that do not match between two tables:table A | table B id | id 1 | 2 2 | 4 3 |1. a list of ids only in the right table... result (4)2. a list of ids only in the left table... result (1, 3)3. a list of ids in the left table... result (1, 3, 4) |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-12-31 : 04:22:35
|
| can we see what you tried till now? |
 |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2008-12-31 : 04:23:07
|
| select * from tabb where id not in (select id from taba)select * from taba where id not in (select id from tabb) |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-12-31 : 04:24:56
|
quote: Originally posted by bklr select * from tabb where id not in (select id from taba)select * from tabb where id not in (select id from taba)taba a on a.id <> b.id
have you tested this. the second one is not even syntactically correct |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-12-31 : 04:26:44
|
This is correct for (1)select * from tabb where id not in (select id from taba)Why not simply change sources so that you also handle (2)select * from taba where id not in (select id from tabb)And (3)select * from taba E 12°55'05.63"N 56°04'39.26" |
 |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2008-12-31 : 04:26:51
|
quote: Originally posted by visakh16
quote: Originally posted by bklr select * from tabb where id not in (select id from taba)select * from tabb where id not in (select id from taba)taba a on a.id <> b.id
have you tested this. the second one is not even syntactically correct
just copy & paste mistake i have modified the the query |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-12-31 : 04:26:57
|
| [code]1.SELECT idFROM tableB bWHERE NOT EXISTS (SELECT 1 FROM tableA WHERE id = b.id)2.SELECT idFROM tableA aWHERE NOT EXISTS (SELECT 1 FROM tableB WHERE id = a.id)3.SELECT * FROM TableA[/code]suggest you to learn joins from books online. all the above are simple problems which you can get solution easily by understanding joins |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-12-31 : 04:38:41
|
[code]-- Prepare sample dataDECLARE @TableA TABLE ( id INT )INSERT @TableASELECT 1 UNION ALLSELECT 2 UNION ALLSELECT 3DECLARE @TableB TABLE ( id INT )INSERT @TableBSELECT 2 UNION ALLSELECT 4-- Solution for all questions at onceSELECT id, CASE WHEN MIN(tbl) = 'b' THEN 'yes' ELSE 'no' END AS [Table B only], CASE WHEN MAX(tbl) = 'a' THEN 'yes' ELSE 'no' END AS [Table A only], CASE WHEN MIN(tbl) = 'a' THEN 'yes' ELSE 'no' END AS [Table A]FROM ( SELECT id, 'a' AS tbl FROM @TableA UNION ALL SELECT id, 'b' FROM @TableB ) AS dGROUP BY idORDER BY id[/code] E 12°55'05.63"N 56°04'39.26" |
 |
|
|
asafg
Starting Member
39 Posts |
Posted - 2008-12-31 : 04:56:14
|
| I have very big tablesthis query that shows what is equal finish its execution in less then a second for 146k rowsselect * from(select IDNo from db.dbo.table1 where RunCode=2) as ainner join(select IDNo from db.dbo.table2 where RunCode=2) as b on a.IDNo=b.IDNousing the exist given by visakh16 does a great job but it takes over 3 secondsSomeone had told me in the past to make inner join manipulation like create a temp table and delete the inner join(the record that match) - and it works - It just look so twisted that I looked for another method with equal performancevisakh16 - thank you so much but it was a bit lazy to ask for 1 and two when they are the same but in three I was looking for a join of the results of 1 and two - which when I thing about it over was lazy two it can't be dome in one shot and requires a union.Thanks Asaf |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-12-31 : 05:01:45
|
| welcome |
 |
|
|
Jai Krishna
Constraint Violating Yak Guru
333 Posts |
Posted - 2008-12-31 : 06:30:21
|
| select * from tabb a where not exists (select * from taba b where a.id = b.id)select * from taba a where not exists (select * from tabb b where a.id = b.id)select * from tabaJai Krishna |
 |
|
|
|