title: When column values are equal then value, otherwise NULL while GROUPING
body: <p>I have the following table:</p>
  <pre><code>DECLARE @tbl TABLE
  (
      Col_1 VARCHAR(50),
      Col_2 VARCHAR(50),
      Col_3 VARCHAR(50)
  );
  </code></pre>
  <p>and sample data:</p>
  <pre><code>INSERT INTO @tbl
  (
      Col_1,
      Col_2,
      Col_3
  )
  VALUES
    ('1', '2', '3')
  , ('1', '2', '3')
  , ('1', '21', '4')
  , ('1', '21', '5')
  , ('1', '31', '8')
  , ('1', '31', '8')
  , ('1', '31', '8')
  , ('3', '41', '1')
  , ('3', '41', '2')
  </code></pre>
  <p>What I want is to get <code>col_3</code> when all values are equal, otherwise it should be <code>NULL</code>, when I group by <code>col_1</code>, <code>col_2</code>. So the desired result looks like this:</p>
  <pre><code>Col_1   Col_2   Col_3
  1         2      3
  1         21     NULL
  1         31     8
  3         41     NULL 
  </code></pre>
  <p>What I've managed to do is just to count unique values, however I cannot get the value of <code>col_3</code></p>
  <pre><code>SELECT 
    t.Col_1
  , t.Col_2
  , COUNT(DISTINCT t.Col_3) AS DistinctCount
  FROM @tbl AS t
  GROUP BY t.Col_1,
           t.Col_2
           
  </code></pre>
  <p>Any help would be greatly appreciated!</p>