A Query to remove relationships that do not belong [closed]
- by Segfault
In a SQL Server 2008 R2 database, given this schema:
AgentsAccounts
_______________
AgentID int UNIQUE
AccountID
FinalAgents
___________
AgentID
I need to create a query that does this: For each AgentID 'final' in FinalAgents remove all of the OTHER AgentID's from AgentsAccounts that have the same AccountID as 'final'. So if the tables have these rows before the query:
AgentsAccounts
AgentID AccountID
1 A
2 A
3 B
4 B
FinalAgents
1
3
then after the query the AgentsAccounts table will look like this:
AgentsAccounts
AgentID AccountID
1 A
3 B
What T-SQL query will delete the correct rows without using a curosr?