title: HOWTO: Include column that is not part of an aggregate function or Group by clause in SQL SERVER
body: <p>I have below recursive CTE:</p>
  
  <pre><code>DECLARE @T AS TABLE
  (
      PARENT_TEST_ID int,
      TEST_ID  int,
      VALIDATED int,
      ERR int
  )
  
  INSERT INTO @T VALUES
  (NULL, 1, 0, 0),
  (NULL, 2, 0, 0),
  (1,3,0, 0),
  (1,4,0, 0),
  (2,5,0, 0),
  (2,6,0, 0),
  (2,7,0, 0),
  (7,8,0, 1)
  
  ;with C as
  (
    select TEST_ID, PARENT_TEST_ID, (CASE WHEN ERR=1 THEN 0 ELSE 1 END) AS VALIDATED, ERR
    from @T
    where TEST_ID not in (select PARENT_TEST_ID 
                     from @T 
                     where PARENT_TEST_ID is not null) AND PARENT_TEST_ID IS NOT NULL
    union all
    select 
    T.TEST_ID, 
    T.PARENT_TEST_ID, 
    (case when t.TEST_ID=c.PARENT_TEST_ID and c.VALIDATED=1 AND T.ERR=0 THEN 1 ELSE 0 END) as VALIDATED,
    T.ERR
    from @T as T
      inner join C
        on T.TEST_ID = C.PARENT_TEST_ID 
  )
  SELECT DISTINCT PARENT_TEST_ID, TEST_ID, MIN(VALIDATED) FROM C
  GROUP BY TEST_ID
  </code></pre>
  
  <p>But I cannot include PARENT_TEST_ID column in the result SELECT as it is not part of the group by clause, so I have found this link:</p>
  
  <p><a href="https://stackoverflow.com/questions/15060604/sql-server-include-column-that-is-not-part-of-an-aggregate-function-or-group-by">Including column that is not part of the group by</a></p>
  
  <p>So now I am trying to do the same in my case, I am trying to apply John Woo solution but I do not know how. Any help? Or any other best solution?</p>