title: How to Order BY without having Column in SELECT
body: <p>I am using MS-SQL and I have a table like this:</p>
  
  <pre><code>Count  Code
  1      A
  2      C
  3      A
  4      B
  5      C
  6      B
  </code></pre>
  
  <p>I need to only to get Distinct Row of the Code Column but I need to keep the Order of the Column to create a Comma Delimited String like this:</p>
  
  <pre><code>    A, C, B
  </code></pre>
  
  <p>I have tried the following code to not get an SQL Error of Msg 145, Level 15 - ORDER BY items must appear in the select list if SELECT DISTINCT is specified.</p>
  
  <pre><code>SELECT @tlist = ISNULL(@tlist+', ','') + Code 
  FROM (SELECT DISTINCT t.Code  
       FROM @Table t) 
  </code></pre>
  
  <p>but I get an output of A, B, C</p>