Note that there are some explanatory texts on larger screens.

plurals
  1. POSQL query to return difference between table joined to itself
    primarykey
    data
    text
    <p>I'm using SQL Server 2012.</p> <p>I have a view called <strong>AdvApp.vPortfolioGroupMemberFlattened</strong> I want to return all memberid's that are in portfoliogroupcode <strong>master</strong> or portfoliogroupcode <strong>open</strong> but are not in both groups. I don't care about members in other groups.</p> <p>The following stackoverflow article is close to what I need, but doesn't seem to work when joining a table to itself.</p> <p><a href="https://stackoverflow.com/questions/2077807/sql-query-to-return-differences-between-two-tables">sql query to return differences between two tables</a>. </p> <p>This is my table.</p> <pre><code>PortfolioGroupCode MemberID MemberCode master 316 abc master 317 def open 316 abc open 317 def open 321 ghi master 322 jkl closed 89 lmn </code></pre> <p>This is my desired result showing the delta between members of master and open.</p> <pre><code>PortfolioGroupCode MemberID MemberCode open 321 ghi master 322 jkl </code></pre> <p>I tried the following two queries, both from the aforementioned article.</p> <pre><code>SELECT A.*, B.* FROM [AdvApp].[vPortfolioGroupMemberFlattened] a FULL JOIN [AdvApp].[vPortfolioGroupMemberFlattened] b ON (A.PortfolioGroupCode = B.PortfolioGroupCode) WHERE A.PortfolioGroupCode IS NULL OR B.PortfolioGroupCode IS NULL </code></pre> <p>AND</p> <pre><code>SELECT DISTINCT a.membercode, a.portfoliogroupcode FROM [AdvApp].[vPortfolioGroupMemberFlattened] a WHERE ( NOT EXISTS ( SELECT b.membercode FROM [AdvApp].[vPortfolioGroupMemberFlattened] b WHERE a.PortfolioGroupCode = 'master' AND b.PortfolioGroupCode = 'open' )) </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.
 

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