Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Updated to use your newly provided data:</strong></p> <p>The solutions using the original data may be found at the end of this answer.</p> <p>Using your new data:</p> <pre><code>DECLARE @T TABLE( [contract] INT, project INT, activity INT ) INSERT INTO @T VALUES( 1000, 8000, 10 ) INSERT INTO @T VALUES( 1000, 8000, 20 ) INSERT INTO @T VALUES( 1000, 8001, 10 ) INSERT INTO @T VALUES( 2000, 9000, 49 ) INSERT INTO @T VALUES( 2000, 9001, 49 ) INSERT INTO @T VALUES( 3000, 9000, 79 ) INSERT INTO @T VALUES( 3000, 9000, 78 ) SELECT DISTINCT [contract], activity FROM @T AS A WHERE (SELECT COUNT( DISTINCT activity ) FROM @T AS B WHERE B.[contract] = A.[contract]) = 1 </code></pre> <p>returns: 2000, 49</p> <p><strong>Solutions using original data</strong></p> <p><strong>WARNING:</strong> The following solutions use the data previously given in the question and may not make sense for the current question. I have left them attached for completeness only.</p> <pre><code>SELECT Col1, Count( col1 ) AS count FROM table GROUP BY col1 HAVING count &gt; 1 </code></pre> <p>This should get you a list of all the values in col1 that are not distinct. You can place this in a table var or temp table and join against it.</p> <p>Here is an example using a sub-query:</p> <pre><code>DECLARE @t TABLE( col1 VARCHAR(1), col2 VARCHAR(1), col3 VARCHAR(1) ) INSERT INTO @t VALUES( 'A', 'B', 'C' ); INSERT INTO @t VALUES( 'D', 'E', 'F' ); INSERT INTO @t VALUES( 'A', 'J', 'K' ); INSERT INTO @t VALUES( 'G', 'H', 'H' ); SELECT * FROM @t SELECT col1, col2 FROM @t WHERE col1 NOT IN (SELECT col1 FROM @t AS t GROUP BY col1 HAVING COUNT( col1 ) &gt; 1) </code></pre> <p>This returns:</p> <pre><code>D E G H </code></pre> <p>And another method that users a temp table and join:</p> <pre><code>DECLARE @t TABLE( col1 VARCHAR(1), col2 VARCHAR(1), col3 VARCHAR(1) ) INSERT INTO @t VALUES( 'A', 'B', 'C' ); INSERT INTO @t VALUES( 'D', 'E', 'F' ); INSERT INTO @t VALUES( 'A', 'J', 'K' ); INSERT INTO @t VALUES( 'G', 'H', 'H' ); SELECT * FROM @t DROP TABLE #temp_table SELECT col1 INTO #temp_table FROM @t AS t GROUP BY col1 HAVING COUNT( col1 ) = 1 SELECT t.col1, t.col2 FROM @t AS t INNER JOIN #temp_table AS tt ON t.col1 = tt.col1 </code></pre> <p>Also returns:</p> <pre><code>D E G H </code></pre>
    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. 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