title: How to select tree from multiple columns
body: <p>Have data like this </p>
  
  <pre><code>DECLARE @tbl TABLE (name1 varchar(5),name2 varchar(5),name3 varchar(5))
  INSERT INTO @tbl(name1,name2,name3) 
  values ('P1','A1','AA1'),('P1','B1','AA2'),('P1','C1','AA3'),('P1','A1','AA4')
  </code></pre>
  
  <p>Input table is:</p>
  
  <pre><code>name1   name2   name3
  P1        A1    AA1
  P1        B1    AA2
  P1        C1    AA3
  P1        A1    AA4
  </code></pre>
  
  <p>I want to select from this a little bit strange tree, result must be like this:</p>
  
  <pre><code>col1    col2
   P1      A1
   A1      AA1
   A1      AA4
   P1      B1
   B1      AA2
   P1      C1
   C1      AA4
  </code></pre>
  
  <p>Example for first rows <code>'P1','A1','AA1'</code> <code>P1</code> is root for <code>A1</code>, <code>A1</code> is root for <code>AA1</code> but also <code>A1</code> is root for <code>AA4</code> in <code>'P1','A1','AA4'</code>.That's why output must be like: </p>
  
  <pre><code>P1      A1
  A1      AA1
  A1      AA4
  </code></pre>
  
  <p>Sqlfiddle : <a href="http://www.sqlfiddle.com/#!18/87eb3/1" rel="nofollow noreferrer">http://www.sqlfiddle.com/#!18/87eb3/1</a></p>