| Author |
Topic |
|
agarwasa2008
Posting Yak Master
109 Posts |
Posted - 2009-10-20 : 20:16:10
|
| A new question. I have 3 tables and a sql query. I need to check table2.OrdID against the table3.OrderingCode to make sure it is a valid code before I update table1. Please advice. I added left outer join. But the results give me the same # of rows even though there are a few invalid ids in table2. So the results does not seem correct. Table 1: (new master table)ProductIDorderID DescriptionTable2: (Legacy table - old data which needs to be transferred to the new table)ProdIDordIDTable 3: (master table) OrderingCodedesc-- inserts 418 rowsINSERT INTO Table1 (ProductID, orderID)SELECT ProdID, ordIDFROM Table2 t2left outer join dbo.Table3 as a on a.OrderingCode=t2.ordIDWHERE NOT EXISTS (SELECT * FROM Table1 t1 WHERE t1.ProductID = t2.ProdID AND t1.orderID = t2.ordID)SA |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2009-10-20 : 20:50:34
|
| INSERT INTO Table1 (ProductID, orderID)SELECT t2.ProdID, t2.ordIDFROM Table1 t1LEFT JOIN Table2 t2ON t1.ProductID = t2.ProdID AND t1.orderID = t2.ordIDWHERE isnull(t2.productId,t2.orderid) is nulland t2.ordid in (select orderingCode from table3) ?Jim Everyday I learn something that somebody else already knew |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-10-21 : 12:32:07
|
| isnt it just a matter ofINSERT INTO Table1 (ProductID, orderID)SELECT ProdID, ordIDFROM Table2 t2inner join dbo.Table3 as a on a.OrderingCode=t2.ordID |
 |
|
|
agarwasa2008
Posting Yak Master
109 Posts |
Posted - 2009-10-21 : 13:45:09
|
| Vishak i tried your query but it does not work. It gives me 11 records that can't be correct. I am expecting at least 3000 plus records.SA |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-10-21 : 13:50:22
|
| how many records does table 2 & table 3 have? |
 |
|
|
agarwasa2008
Posting Yak Master
109 Posts |
Posted - 2009-10-21 : 13:51:53
|
| Ps: I am just using the table 3 to check the validity of ordID. Table3 is the master table and if Table2.ordID exists in Table3. It is valid and we want it in our resultset.SA |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-10-21 : 13:54:10
|
| and if not present in t3 you want to ignore? |
 |
|
|
agarwasa2008
Posting Yak Master
109 Posts |
Posted - 2009-10-21 : 13:55:27
|
| okay. table2 = 1386; table3= 4184 and the query below gives 405select ordID from table2 where ordID in (select distinct OrderingCode from table3)SA |
 |
|
|
agarwasa2008
Posting Yak Master
109 Posts |
Posted - 2009-10-21 : 13:56:30
|
| and if not present in t3 you want to ignore?YES!!!SA |
 |
|
|
agarwasa2008
Posting Yak Master
109 Posts |
Posted - 2009-10-21 : 14:10:41
|
| Should it be an inner, outer, left join in the scenario for table3 . Just an inner join should work in the general case but using it in this query does not give the right # of records. Please advice!!INSERT INTO Table1 (ProductID, orderID)SELECT ProdID, ordIDFROM Table2 t2inner join dbo.Table3 as a on a.OrderingCode=t2.ordIDWHERE NOT EXISTS (SELECT * FROM Table1 t1 WHERE t1.ProductID = t2.ProdID AND t1.orderID = t2.ordID) |
 |
|
|
|