Note that there are some explanatory texts on larger screens.

plurals
  1. POOptimize TSQL Except
    primarykey
    data
    text
    <p>I could optimize the join if I could include the ID from table <code>MasterStaging</code> (staging table):</p> <ul> <li>Including the ID in the <code>EXCEPT</code> will distort the results since the <code>ID</code> from <code>MasterStaging</code> will always be different from <code>StatusComparison</code></li> <li><code>MasterStaging.ID</code> is irrelevant, just an autonumber, and does not represent the customer ID</li> <li>The SQL is designed to show missing customers, regardless of ID. </li> <li>The CustomerAccountNo is not a unique number</li> </ul> <p>How can I include obtain the ID top optimize the <code>JOIN</code>?</p> <p>This is what I want:</p> <p>Same SQL as below, but I will <code>JOIN</code> only using <code>ID</code></p> <pre><code>) x ON e.ID = x.ID </code></pre> <p>Here is what I have so far:</p> <pre><code>UPDATE ecl.MasterStaging SET NewAccount = 1 FROM ecl.MasterStaging e WITH (NOLOCK) JOIN ( SELECT ISNULL(Usable, 0) AS Usable , ISNULL(TypeRC, 0) AS TypeRC , ISNULL(CustomerNumber, 0) AS CustomerNumber , ISNULL(CustomerAccountNo, 0) AS CustomerAccountNo , ISNULL(LoadProfileClass, 0) AS LoadProfileClass , ISNULL(MeterNo, 0) AS MeterNo , ISNULL(PrimaryPhoneNumber, 0) AS PrimaryPhoneNumber , ISNULL(CustomerName1, 0) AS CustomerName1 , ISNULL(ServiceAddress1, 0) AS ServiceAddress1 , ISNULL(ServiceCity, 0) AS ServiceCity , ISNULL(ServiceState, 0) AS ServiceState , ISNULL(ServiceZip, 0) AS ServiceZip , ISNULL(BillingAddress1, 0) AS BillingAddress1 , ISNULL(BillingCity, 0) AS BillingCity , ISNULL(BillingState, 0) AS BillingState , ISNULL(substring(BillingZip, 1, 5), 0) as BillingZip , ISNULL(substring(BillingZip4, 7, 4), 0) as BillingZip4 FROM ecl.MasterStaging WITH (NOLOCK) EXCEPT SELECT Usable , TypeRC , CustomerNumber , CustomerAccountNo , LoadProfileClass , MeterNo , PrimaryPhoneNumber , CustomerName1 , ServiceAddress1 , ServiceCity , ServiceState , ServiceZip , BillingAddress1 , BillingCity , BillingState , BillingZip , BillingZip4 FROM ecl.StatusComparison WITH (NOLOCK) WHERE [Status] &lt;&gt; 'D' ) x ON ISNULL(e.Usable,0) = x.Usable AND ISNULL(e.TypeRC,0) = x.TypeRC AND ISNULL(e.CustomerNumber,0) = x.CustomerNumber AND ISNULL(e.CustomerAccountNo,0) = x.CustomerAccountNo AND ISNULL(e.LoadProfileClass,0) = x.LoadProfileClass AND ISNULL(e.MeterNo,0) = x.MeterNo AND ISNULL(e.PrimaryPhoneNumber,0) = x.PrimaryPhoneNumber AND ISNULL(e.CustomerName1,0) = x.CustomerName1 AND ISNULL(e.ServiceAddress1,0) = x.ServiceAddress1 AND ISNULL(e.ServiceCity,0) = x.ServiceCity AND ISNULL(e.ServiceState,0) = x.ServiceState AND ISNULL(e.ServiceZip,0) = x.ServiceZip AND ISNULL(e.BillingAddress1,0) = x.BillingAddress1 AND ISNULL(e.BillingCity,0) = x.BillingCity AND ISNULL(e.BillingState,0) = x.BillingState AND ISNULL(e.BillingZip,0) = x.BillingZip AND ISNULL(e.BillingZip4,0) = x.BillingZip4 </code></pre>
    singulars
    1. This table or related slice is empty.
    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. 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