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 |
|
kporg
Starting Member
5 Posts |
Posted - 2007-08-14 : 17:59:48
|
| New to the forum. Any help is greatly appreciated!Here is what I am trying to do...table Acustn, ord#Joe 1Bob 2Sally 3Pete 4Jill 5table Bord# prod#1 11 22 13 1I want to return the custn from table A where table B has a prod#=1 but not a prod#=2. So it should only return Bob and Sally. |
|
|
dinakar
Master Smack Fu Yak Hacker
2507 Posts |
Posted - 2007-08-14 : 18:17:13
|
| [code]Declare @tableA Table (custn varchar(10), ord int)Insert into @TableASelect 'Joe', 1 union allSelect 'Bob' ,2 union allSelect 'Sally', 3 union allSelect 'Pete' ,4 union allSelect 'Jill' ,5Declare @tableB Table (ord int,prod int)insert into @TableBSelect 1, 1 union allSelect 1, 2 union allSelect 2, 1 union allSelect 3 ,1Select A2.* from ( Select A.Custn, A.ord from @tableA A Join @TableB B ON A.Ord = B.Ord Where B.Prod = 1 ) A2 Left Outer Join (Select Ord From @TableB Where Prod = 2) B2 on a2.ord = b2.ordWhere B2.ord is null[/code]or [code]Select A.Custn, A.ord from @tableA AJoin @TableB B ON A.Ord = B.OrdWhere B.Prod = 1 And A.Ord Not in (Select Ord From @TableB Where Prod = 2)[/code]Dinakar Nethi************************Life is short. Enjoy it.************************http://weblogs.sqlteam.com/dinakar/ |
 |
|
|
kporg
Starting Member
5 Posts |
Posted - 2007-08-15 : 12:11:34
|
| Thank you very much. I think I got it working with the 2nd query |
 |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
|
|
|
|
|
|
|