title: How to group/List all nodes of a undirected graph using teradata sql
body: <p>I have data for many diff. set of undirected graphs in a table (like adjacent list relationship, one node is connected which all node) and I need to group all individual undirected graphs.</p>
  
  <p>Eg: all nodes of the particular undirected graphs will be in a group &amp; group name will be the min. of the node.</p>
  
  <pre><code>sel d.adj_node, min(d.adj_node) Over (Partition By a.node) as grp
  table a 
  left join table b
  on a.adj_node=b.node
  left join table c
  on b.adj_node=c.node
  ​left join table d
  ​on c.adj_node=d.node​;
  </code></pre>
  
  <p>Now, I am doing a self-join for 4,5 times and then on top that query doing partitioning it to get the desired output. But doing self-join 4 5 times is creating performance issue. </p>
  
  <p>So, need some recursive sql, stored procedure or some other logic to do the same for all levels. Input Data &amp; Required Output will be like this <a href="http://www.digitalcrunch.in/uploads/Capture1.JPG" rel="nofollow">link</a> Looking for some suggestions. </p>
  
  <pre><code>Input Table
  
  node    adj_node
  1       2
  2       1
  2       3
  2       5
  2       6
  2       7
  3       2
  3       4
  4       3
  4       5
  4       6
  4       7
  5       2
  5       4
  6       2
  6       4
  6       8
  7       2
  7       4
  8       6
  1       1
  2       2
  3       3
  4       4
  5       5
  6       6
  7       7
  8       8
  10      11
  11      10
  11      13
  11      14
  12      13
  12      14
  13      11
  13      12
  13      14
  14      11
  14      12
  14      13
  10      10
  11      11
  12      12
  13      13
  14      14
  
  
  Output
  node    grp
  1       1
  2       1
  3       1
  4       1
  5       1
  6       1
  7       1
  8       1
  10      10
  11      10
  12      10
  13      10
  14      10
  </code></pre>