Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>SQL Server</code> lacks native bitwise aggregates, that's why we need to emulate them.</p> <p>The main idea here is to generate a set of bits from <code>0</code> to <code>15</code>, for each bit apply the bitmask to the value and select <code>MAX</code> (which will give us an <code>OR</code> for a given bit), then select the <code>SUM</code> (which will merge the bit masks).</p> <p>The we just update the first <code>lngInternetPK</code> for any given <code>(lngContactFK, strValue)</code> with the new value of <code>lngValue</code>, and delete all duplicates.</p> <pre><code>;WITH bits AS ( SELECT 0 AS b UNION ALL SELECT b + 1 FROM bits WHERE b &lt; 15 ), v AS ( SELECT i.*, ( SELECT SUM(value) FROM ( SELECT MAX(lngValue &amp; POWER(2, b)) AS value FROM tblmInternet ii CROSS JOIN bits WHERE ii.lngContactFK = i.lngContactFK AND ii.strAddress = i.strAddress GROUP BY b ) q ) AS lngNewValue FROM ( SELECT ii.*, ROW_NUMBER() OVER (PARTITION BY lngContactFK, strAddress ORDER BY lngInternetPK) AS rn FROM tblmInternet ii ) i WHERE rn = 1 ) UPDATE v SET lngValue = lngNewValue; ;WITH v AS ( SELECT ii.*, ROW_NUMBER() OVER (PARTITION BY lngContactFK, strAddress ORDER BY lngInternetPK) AS rn FROM tblmInternet ii ) DELETE v WHERE rn &gt; 1 </code></pre> <p>See this article in my blog for more detailed explanations:</p> <ul> <li><a href="http://explainextended.com/2009/07/13/sql-server-aggregate-bitwise-or/" rel="nofollow noreferrer"><strong>SQL Server: aggregate bitwise OR</strong></a></li> </ul>
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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