Filter OUTER APPLY column in WHERE clause
<p>I currently have a complex SQL query which is inserted into a temp table. The query includes an <code>OUTER APPLY</code> as not all returned records will apply to the result set.</p>

<p>I also need to use the <code>OUTER APPLY</code> columns in the <code>WHERE</code> clause to filter results but also include the results which do not apply into the <code>OUTER APPLY</code> .e. All Outer APPLY results = 1 and non-outer apply results. </p>

<p>This is a simple version of the query layout:</p>

<pre><code>INSERT INTO #temp (X, Y, Z, O1, O2)
SELECT 
    X Y Z
FROM T1 
INNER JOIN T2, T etc.
OUTER APPLY (
    SELECT O1, O2 FROM XYZ…) OATable
WHERE
    OATable.O1 = 1 -- I tried just adding “IN (1, NULL)” but this 
still excludes the results. 
</code></pre>

<p>Any help would be greatly appreciated. </p>

<p>Platform: SQL Server 2012+</p>

<p>Thank you</p>