| Author |
Topic |
|
pelegk2
Aged Yak Warrior
723 Posts |
Posted - 2006-11-06 : 06:46:49
|
| i have 2 tables :table 1 :companyid,personalid,stocktypetable2companyid,stocktypei want to make a query with all thecompanyid that exist in table2, and to check in which of the companies exist stocktype in table 1 but NOT in table2?how can i d o this?thnaks in advancepelegIsrael -the best place to live in aftr heaven 9but no one wan't to go there so fast -:) |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-11-06 : 06:49:18
|
| Care to post some sample data and expectd output?Peter LarssonHelsingborg, Sweden |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-11-06 : 06:50:54
|
| select table2.* from table2 left join table1 on table1.companyid = table2.companyid and table2.stocktype = table1.stocktypewhere table1.companyid is nullPeter LarssonHelsingborg, SwedenEDIT: Changed from Table2 to Table1 and back for the people who can't do that themselfselect table1.* from table1 left join table2 on table2.companyid = table1.companyid and table2.stocktype = table1.stocktypewhere table2.companyid is null |
 |
|
|
chiragkhabaria
Master Smack Fu Yak Hacker
1907 Posts |
Posted - 2006-11-06 : 06:51:08
|
| [code]Select * From Table2 tbl1 Left outer join Table2 tbl2 on tbl2.stocktype = tbl1.stocktype where tbl1.stocktype is null[/code]Chiraghttp://chirikworld.blogspot.com/ |
 |
|
|
pelegk2
Aged Yak Warrior
723 Posts |
Posted - 2006-11-06 : 07:04:34
|
Peso : i at start did as yourquery but if you wil ltry you will see that stocktype that dosent appear on table 2 but exist in table1 wont appear in the query results!!!! - and that what i look for!chiragkhabaria : your quesy isnt good beacuse i for each comany in sepeart need to do the stocktype check!a data sample : table1 :companyid personalid stocktype555 123 15555 365 99555 952 44table2 :companyid stocktype555 15555 44 in this case stocktype 99 which exist in table1 for companyid : 555dosent exist in table2 and that i look for!thnaks in advancepelegIsrael -the best place to live in aftr heaven 9but no one wan't to go there so fast -:) |
 |
|
|
mightypenny_ph
Yak Posting Veteran
54 Posts |
Posted - 2006-11-06 : 07:06:19
|
| SELECT D1.companyID, D1.PersonalID, D2.StockType FROM table1 D1 INNER JOIN table2 D2ON D1.companyID = D2.companyID WHERE D1.StockType IS NULLhope this helps.... I maybe wrong... Please try it out...SlayerS_`BoxeR` + [ReD]NaDa |
 |
|
|
chiragkhabaria
Master Smack Fu Yak Hacker
1907 Posts |
Posted - 2006-11-06 : 07:07:38
|
| Somthing like this ?Select * From Table2 tbl2 Left outer join Table1 tbl1 on tbl2.stocktype = tbl1.stocktype AND tbl2.companyid = tbl1.companyidwhere tbl1.stocktype is nullChiraghttp://chirikworld.blogspot.com/ |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-11-06 : 07:15:08
|
Besides my previous query (which only needs to change from table1 to table2) this also works.select t1.*from @table1 t1where not exists (select * from @table2 t2 where t2.companyid = t1.companyid and t2.stocktype = t1.stocktype) Peter LarssonHelsingborg, Sweden |
 |
|
|
pelegk2
Aged Yak Warrior
723 Posts |
Posted - 2006-11-06 : 07:47:54
|
| chiragkhabaria : test it an u will see that u wont get the missing stocktypePeso : your code almost ok except for 1 thing : i need that in table2 that at least 1 companyid for each company will exist before i compare,so if some companyid dosent exist at all on table 2 so there will be no compare with table 1thnaks in advancepeleg Israel -the best place to live in aftr heaven 9but no one wan't to go there so fast -:) |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-11-06 : 08:36:14
|
You mean like you have sample data like this?table1 :companyid personalid stocktype555 123 15555 365 99555 952 44556 887 44table2 :companyid stocktype555 15555 44 And the only valid return value is 555 365 99 ?Peter LarssonHelsingborg, Sweden |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-11-06 : 08:41:14
|
If you put in a little effort yourself, you will see that an easy extra WHERE will do the trick...select t1.*from table1 t1left join table2 t2 on t2.companyid = t1.companyid and t2.stocktype = t1.stocktypewhere t2.companyid is null and t1.companyid in (select t.companyid from table2 t) Peter LarssonHelsingborg, Sweden |
 |
|
|
pelegk2
Aged Yak Warrior
723 Posts |
Posted - 2006-11-07 : 01:34:00
|
PESO :quote: And the only valid return value is 555 365 99?
yep i just need the companyid that have the stocktype diffrence!but if u will check your query it dosnet get the goal that i need beacuse it give you all the stocktype from table2 that dosent exist for each company in table while i try to get the oppoiste goal!making an inner join from table2 to table1 with the example i gave u wont give u any result beacuse the 2 type in table 2 exist already in table1!Israel -the best place to live in aftr heaven 9but no one wan't to go there so fast -:) |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-11-07 : 01:44:00
|
| Tell me what is wrong with my last suggestion and provide some more sample data.Also provide and explain the expected output base don the supplied sample data.Peter LarssonHelsingborg, Sweden |
 |
|
|
pelegk2
Aged Yak Warrior
723 Posts |
Posted - 2006-11-07 : 11:03:16
|
| your last query is great:)imeant about the one before our answers get little mixtared and maybe i hae answred beore i have seen your new answer beacusei h ave answred withought refreshingIsrael -the best place to live in aftr heaven 9but no one wan't to go there so fast -:) |
 |
|
|
|