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 |
|
codewzrd
Starting Member
8 Posts |
Posted - 2004-11-05 : 11:15:35
|
| [code]table1 table2idxPK idxFK Type Date idxPK Name----- ----- ---- ---- ----- --------1 1 1001 10/02/04 1 John2 1 1002 10/20/04 2 Mike3 1 1002 10/14/04 3 Sam4 2 1001 10/22/045 3 1001 09/29/046 1 1001 09/09/04[/code]The table1 has a foreign key to table2. Type represents owe (1001, customer owes company money) or credit (1002, company overcharged customer).The query that I want to create is to extract the Name from table 2 where that name has both a owe (1001) and a credit (1002) in a certain date range (month of october).[code]SELECT DISTINCT Table2.NameFROM Table2 INNER JOIN Table1 ON Table2.idxPK = Table1.idxFKWHERE ???[/code]Result should be JohnI can't do an AND in the WHERE CLAUSE "WHERE Type = 1001 AND Type = 1002". That can never be. And if I do an OR "WHERE Type = 1001 OR Type = 1002", I get all Names.Any help would be appreciated. |
|
|
AndrewMurphy
Master Smack Fu Yak Hacker
2916 Posts |
Posted - 2004-11-05 : 11:25:33
|
| select b.name from table2 binner join table1 a on b.idxPK = a.idxPK and a.type = 1001inner join table1 c on b.idxPK = c.idxPK and c.type = 1002 |
 |
|
|
codewzrd
Starting Member
8 Posts |
Posted - 2004-11-05 : 12:22:32
|
| Awesome! Thank you. |
 |
|
|
|
|
|