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 |
ddouma
Starting Member
10 Posts |
Posted - 2007-01-30 : 09:10:40
|
Hi, I'm hoping that someone could help me out. Basically I need to select the rows from TABLE 1, that don't have a match in TABLE 2 where ENV_ID = 43. So for the example data below, I'd like to return HS_ID = 5TABLE 1--------HS_ID NAME3 WKS014 WKS025 WKS03TABLE 2--------ENV_ID HS_ID40 440 343 443 340 5 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-01-30 : 09:17:46
|
select t1.*from @table1 as t1left join @table2 as t2 on t2.hs_id = t1.hs_id and t2.env_id = 43where t2.hs_id is nullPeter LarssonHelsingborg, Sweden |
 |
|
ddouma
Starting Member
10 Posts |
Posted - 2007-01-30 : 09:29:00
|
Thanks for the quick reply Peter - that was it! |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-01-30 : 09:32:27
|
[code]-- prepare sample datadeclare @TABLE1 table (HS_ID int, NAME sysname)insert @table1select 3, 'WKS01' union allselect 4, 'WKS02' union allselect 5, 'WKS03'declare @table2 table (env_id int, hs_id int)insert @table2select 40, 4 union allselect 40, 3 union allselect 43, 4 union allselect 43, 3 union allselect 40, 5-- show the resultselect t1.*from @table1 as t1left join @table2 as t2 on t2.hs_id = t1.hs_id and t2.env_id = 43where t2.hs_id is null[/code]Peter LarssonHelsingborg, Sweden |
 |
|
|
|
|
|
|