Zaktualizuj kolumnę, aby były różnymi wartościami agregowanymi

Tworzę skrypt, który „łączy” i usuwa duplikaty wierszy z tabeli. Tabela zawiera informacje o adresie i używa pola liczby całkowitej do przechowywania informacji o wiadomości e-mail jako flag bitowych (nazwa kolumny lngValue). Na przykład lngValue i 1 == 1 oznaczają jego adres główny.

Istnieją dwa razy takie same wiadomości e-mail, ale czasami z różnymi wartościami lngValues. Aby rozwiązać ten problem, muszę pobrać lngValue ze wszystkich duplikatów i przypisać je do jednego istniejącego rekordu i usunąć resztę.

Moim największym bólem głowy było „połączenie” nagrań. To, co chcę zrobić, to bitowe lub wszystkie lngValues ​​duplikatów rekordów razem. Oto, co mam do tej pory, które znajduje wartość wszystkich bitów lub bitów razem.

Ostrzeżenie: naprzód niechlujny kod

declare @duplicates table
(
lngInternetPK int,
lngContactFK int,
lngValue int
)

insert into @duplicates (lngInternetPK, lngContactFK, lngValue) 
(
select  tblminternet.lngInternetPK, tblminternet.lngContactFK, tblminternet.lngValue   from tblminternet  inner join 
(select strAddress, lngcontactfk, count(*) as count from tblminternet where lngValue & 256 <> 256 group by strAddress, lngcontactfk) secondemail
On tblminternet.strAddress = secondemail.strAddress and
tblminternet.lngcontactfk = secondemail.lngcontactfk 
where count > 1 and tblminternet.strAddress is not null and tblminternet.lngValue & 256 <> 256 --order by lngContactFK, strAddress
)

update @duplicates set lngValue = t.val

from 
                (select (sum(dupes.lngValue) & 65535) as val from 
                    (select  here.lngInternetPK,                     here.lngContactFK, here.lngValue from tblminternet here  inner join 
                    (select strAddress, lngcontactfk, count(*) as count from tblminternet where lngValue & 256 <> 256 group by strAddress, lngcontactfk) secondemail
                    On here.strAddress = secondemail.strAddress     and
                    here.lngcontactfk = secondemail.lngcontactfk 
                    where count > 1 and here.strAddress is not      null and here.lngValue & 256 <> 256) dupes, tblminternet this

                where this.lngContactFK = dupes.lngContactFK
                ) t
where lngInternetPK in (select lngInternetPK from @duplicates)    

Edytować:
Zgodnie z wymaganiami tutaj znajdują się przykładowe dane:

Nazwa tabeli: tblminternet
Nazwy kolumn:
lngInternetPK
lngContactFK
lngValue
strAddress

Przykładowy wiersz 1:
lngInternetPK: 1
lngContactFK: 1
lngValue: 33
strAddress: „[email protected]

Przykładowy wiersz 2:
lngInternetPK: 2
lngContactFK: 1
lngValue: 40
strAddress: „[email protected]

Jeśli te dwa zostały tutaj połączone, to pożądany rezultat:
lngInternetPK: 1
lngContactFK: 1
lngValue: 41
strAddress: „[email protected]

Inne niezbędne zasady:
Każdy kontakt może mieć wiele wiadomości e-mail, ale każdy wiersz wiadomości e-mail musi być odrębny (każdy e-mail może być wyświetlany tylko jako jeden wiersz).

questionAnswers(3)

yourAnswerToTheQuestion