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.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Select statement query

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2006-01-24 : 08:36:48
Dave writes "Hi guys, Ok, perhaps I left my brain at home today but I cant seem to figure out how to write the following query:

I have a table (use Northwinds for example), where I want to find Employees who represent territories (NY and IL for example) - The table structure is basic: EmployeeId, TerritoryId
The select I have written wont work because the TerritoryId cant be ANDed, eg: Select EmployeeId FROM EmployeeTerritory WHERE TerritoryId='NY' AND TerritoryId='IL' << this cant work, it' illogical... so I do I structure a query that will return the employees who have BOTH territories in their portfolio, not just one. I have a feeling I am going to have to use HAVING...

Thanks a lot

David."

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-01-24 : 08:48:28
Try this

Select EmployeeId FROM EmployeeTerritory
WHERE TerritoryId in('NY','IL')
group by EmployeeId having count(*)=2


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -