Hi
I have a minor query issue I'm stuck with. I have a n-m table containing relations between contacts and lists.
I need to do a search and replace contact A with B in the table however I may only replace where the if B is not already in the list for a given Database ID.
E.g. Contact 14 must be replaced with Contact 16
Contact 14 exists in list 11, both in database 0 and 1
Contact 16 exists in list 11, in database 0
In this case it is straight forward to replace contact 14 in database 1, however doing that in database 0 would give 2 * 16 in that database which is not allowed. So in that case I must set the new contact to the highest priority (lowest number) of the two and remove the other one.
So the combined unique key is CallListID+ContactID+DatabaseID - priority is just a ranking of the list where priority 1 is the highest.
So if this is the staring point data set
CallListID ContactID Priority DatabaseID ----------- ----------- ----------- ----------- 11 3 1 1 11 7 2 1 11 9 3 1 11 14 4 1 11 7 1 0 11 16 2 0 11 14 3 0
The result should end like this
CallListID ContactID Priority DatabaseID ----------- ----------- ----------- ----------- 11 3 1 1 11 7 2 1 11 9 3 1 11 16 4 1 11 7 1 0 11 16 2 0
OR, if the starting point is this
CallListID ContactID Priority DatabaseID ----------- ----------- ----------- ----------- 11 3 1 1 11 7 2 1 11 9 3 1 11 14 4 1 11 7 1 0 11 14 2 0 11 16 3 0
it should result in this
CallListID ContactID Priority DatabaseID ----------- ----------- ----------- ----------- 11 3 1 1 11 7 2 1 11 9 3 1 11 16 4 1 11 7 1 0 11 16 2 0
CREATE TABLE [dbo].[CallLists_Contacts]( [CallListID] [int] NOT NULL, [ContactID] [int] NOT NULL, [Priority] [int] NOT NULL, [DatabaseID] [int] NOT NULL ) ON [PRIMARY]
Here are some inserts
INSERT INTO [CallLists_Contacts] ([CallListID],[ContactID],[Priority],[DatabaseID])VALUES(3,2,1,1) INSERT INTO [CallLists_Contacts] ([CallListID],[ContactID],[Priority],[DatabaseID])VALUES(3,2,1,0) INSERT INTO [CallLists_Contacts] ([CallListID],[ContactID],[Priority],[DatabaseID])VALUES(4,2,1,1) INSERT INTO [CallLists_Contacts] ([CallListID],[ContactID],[Priority],[DatabaseID])VALUES(4,2,1,0) INSERT INTO [CallLists_Contacts] ([CallListID],[ContactID],[Priority],[DatabaseID])VALUES(5,2,1,1) INSERT INTO [CallLists_Contacts] ([CallListID],[ContactID],[Priority],[DatabaseID])VALUES(5,2,1,0) INSERT INTO [CallLists_Contacts] ([CallListID],[ContactID],[Priority],[DatabaseID])VALUES(6,2,1,1) INSERT INTO [CallLists_Contacts] ([CallListID],[ContactID],[Priority],[DatabaseID])VALUES(6,2,1,0) INSERT INTO [CallLists_Contacts] ([CallListID],[ContactID],[Priority],[DatabaseID])VALUES(7,3,1,1) INSERT INTO [CallLists_Contacts] ([CallListID],[ContactID],[Priority],[DatabaseID])VALUES(7,3,1,0) INSERT INTO [CallLists_Contacts] ([CallListID],[ContactID],[Priority],[DatabaseID])VALUES(8,2,1,1) INSERT INTO [CallLists_Contacts] ([CallListID],[ContactID],[Priority],[DatabaseID])VALUES(8,2,1,0) INSERT INTO [CallLists_Contacts] ([CallListID],[ContactID],[Priority],[DatabaseID])VALUES(9,2,1,1) INSERT INTO [CallLists_Contacts] ([CallListID],[ContactID],[Priority],[DatabaseID])VALUES(9,3,2,1) INSERT INTO [CallLists_Contacts] ([CallListID],[ContactID],[Priority],[DatabaseID])VALUES(9,16,3,1) INSERT INTO [CallLists_Contacts] ([CallListID],[ContactID],[Priority],[DatabaseID])VALUES(9,2,1,0) INSERT INTO [CallLists_Contacts] ([CallListID],[ContactID],[Priority],[DatabaseID])VALUES(9,3,2,0) INSERT INTO [CallLists_Contacts] ([CallListID],[ContactID],[Priority],[DatabaseID])VALUES(9,16,3,0) INSERT INTO [CallLists_Contacts] ([CallListID],[ContactID],[Priority],[DatabaseID])VALUES(10,3,1,1) INSERT INTO [CallLists_Contacts] ([CallListID],[ContactID],[Priority],[DatabaseID])VALUES(10,16,2,1) INSERT INTO [CallLists_Contacts] ([CallListID],[ContactID],[Priority],[DatabaseID])VALUES(11,3,1,1) INSERT INTO [CallLists_Contacts] ([CallListID],[ContactID],[Priority],[DatabaseID])VALUES(10,3,1,0) INSERT INTO [CallLists_Contacts] ([CallListID],[ContactID],[Priority],[DatabaseID])VALUES(11,7,2,1) INSERT INTO [CallLists_Contacts] ([CallListID],[ContactID],[Priority],[DatabaseID])VALUES(11,9,3,1) INSERT INTO [CallLists_Contacts] ([CallListID],[ContactID],[Priority],[DatabaseID])VALUES(11,14,4,1) INSERT INTO [CallLists_Contacts] ([CallListID],[ContactID],[Priority],[DatabaseID])VALUES(11,7,1,0) INSERT INTO [CallLists_Contacts] ([CallListID],[ContactID],[Priority],[DatabaseID])VALUES(15,3,1,1) INSERT INTO [CallLists_Contacts] ([CallListID],[ContactID],[Priority],[DatabaseID])VALUES(15,3,1,0) INSERT INTO [CallLists_Contacts] ([CallListID],[ContactID],[Priority],[DatabaseID])VALUES(11,16,2,0) INSERT INTO [CallLists_Contacts] ([CallListID],[ContactID],[Priority],[DatabaseID])VALUES(10,16,3,0) INSERT INTO [CallLists_Contacts] ([CallListID],[ContactID],[Priority],[DatabaseID])VALUES(11,14,3,0)
Does anyone know how to solve this in a query?
Henry