title: Get Numbers Range from Table in MSSQL
body: <p>I have a table in MSSQL 2008R2:</p>
  
  <pre>    ID  |  PinAddress
  -------------------------------------
     1  |   1
     1  |   2
     1  |   3
     1  |   4
     1  |   5
     1  |   6
     1  |   16
     1  |   31
     2  |   55
     2  |   56
     2  |   57
     2  |   81
     2  |   82
     2  |   83
     2  |   84
     3  |   101
     3  |   102
     3  |   103
     3  |   107
     3  |   108
     3  |   109
  </pre>
  
  <p>What I want is when I search for ID = 1,I want result like</p>
  
  <pre>1-6,16,31</pre>
  
  <p>When I search for ID = 2,I want result like</p>
  
  <pre>55-57,81-84</pre>
  
  <p>When I search for ID = 3,I want result like</p>
  
  <pre>101-103,107-109</pre>
  
  <p>You can use below script to create table and data:</p>
  
  <pre><code>CREATE TABLE PinAddress(ID INT,PinAddress INT)
  INSERT INTO PinAddress values(1,1)
  INSERT INTO PinAddress values(1,2)
  INSERT INTO PinAddress values(1,3)
  INSERT INTO PinAddress values(1,4)
  INSERT INTO PinAddress values(1,5)
  INSERT INTO PinAddress values(1,6)
  INSERT INTO PinAddress values(1,16)
  INSERT INTO PinAddress values(1,31)
  INSERT INTO PinAddress values(2,55)
  INSERT INTO PinAddress values(2,56)
  INSERT INTO PinAddress values(2,57)
  INSERT INTO PinAddress values(2,81)
  INSERT INTO PinAddress values(2,82)
  INSERT INTO PinAddress values(2,83)
  INSERT INTO PinAddress values(2,84)
  INSERT INTO PinAddress values(3,101)
  INSERT INTO PinAddress values(3,102)
  INSERT INTO PinAddress values(3,103)
  INSERT INTO PinAddress values(3,107)
  INSERT INTO PinAddress values(3,108)
  INSERT INTO PinAddress values(3,109)
  </code></pre>
  
  <p>Thanks</p>