Note that there are some explanatory texts on larger screens.

plurals
  1. POUpdate column to be different aggregate values
    primarykey
    data
    text
    <p>I am creating a script that for "merging" and deleting duplicate rows from a table. The table contains address information, and uses an integer field for storing information about the email as bit flags (column name lngValue). For example, lngValue &amp; 1 == 1 means its the primary address. </p> <p>There are instances of the same email being entered twice, but sometimes with different lngValues. To resolve this, I need to take the lngValue from all duplicates and assign them to one surviving record and delete the rest. </p> <p>My biggest headache so far as been with the "merging" of the records. What I want to do is bitwise or all lngValues of duplicate records together. Here is what I have so far, which only finds the value of all lngValues bitwise or'ed together. </p> <p>Warning: messy code ahead </p> <pre><code>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 &amp; 256 &lt;&gt; 256 group by strAddress, lngcontactfk) secondemail On tblminternet.strAddress = secondemail.strAddress and tblminternet.lngcontactfk = secondemail.lngcontactfk where count &gt; 1 and tblminternet.strAddress is not null and tblminternet.lngValue &amp; 256 &lt;&gt; 256 --order by lngContactFK, strAddress ) update @duplicates set lngValue = t.val from (select (sum(dupes.lngValue) &amp; 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 &amp; 256 &lt;&gt; 256 group by strAddress, lngcontactfk) secondemail On here.strAddress = secondemail.strAddress and here.lngcontactfk = secondemail.lngcontactfk where count &gt; 1 and here.strAddress is not null and here.lngValue &amp; 256 &lt;&gt; 256) dupes, tblminternet this where this.lngContactFK = dupes.lngContactFK ) t where lngInternetPK in (select lngInternetPK from @duplicates) </code></pre> <p>Edit:<br> As requested here is some sample data: </p> <p>Table Name: tblminternet<br> Column Names:<br> lngInternetPK<br> lngContactFK<br> lngValue<br> strAddress </p> <p>Example row 1:<br> lngInternetPK: 1<br> lngContactFK: 1<br> lngValue: 33<br> strAddress: "me@myaddress.com" </p> <p>Example row 2:<br> lngInternetPK: 2<br> lngContactFK: 1<br> lngValue: 40<br> strAddress: "me@myaddress.com" </p> <p>If these two were merged here is the desired result:<br> lngInternetPK: 1<br> lngContactFK: 1<br> lngValue: 41<br> strAddress: "me@myaddress.com" </p> <p>Other necessary rules:<br> Each contact can have multiple emails, but each email row must be distinct ( each email can only appear as one row).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload