title: Data manipulation sql result using stuff approach
body: <p>*Edited to add condition if have boxNo &gt; 1 row</p>
  <p>Let say I have table #DOC with result string sql like this :</p>
  <p>this table doesn't have any primary we can assume that polNum and boxNo pair are unique,</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>polNum</th>
  <th>boxNo</th>
  <th>batchNum</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>111111</td>
  <td>null</td>
  <td>qwe1234</td>
  </tr>
  <tr>
  <td>111111</td>
  <td>ff/55</td>
  <td>rto1235</td>
  </tr>
  <tr>
  <td>111111</td>
  <td>ee/59</td>
  <td>yhn1238</td>
  </tr>
  <tr>
  <td>111111</td>
  <td>ww/55</td>
  <td>rto1235</td>
  </tr>
  <tr>
  <td>222222</td>
  <td>dd/58</td>
  <td>jkl1234</td>
  </tr>
  <tr>
  <td>222222</td>
  <td>null</td>
  <td>fgh1234</td>
  </tr>
  <tr>
  <td>333333</td>
  <td>null</td>
  <td>asz1235</td>
  </tr>
  <tr>
  <td>444444</td>
  <td>ff/55</td>
  <td>edc1234</td>
  </tr>
  <tr>
  <td>444444</td>
  <td>tt/58</td>
  <td>qaz1234</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>I need have collection from batchNum and boxNo become like this.</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>polNum</th>
  <th>boxNo</th>
  <th>batchNum</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>111111</td>
  <td>null</td>
  <td></td>
  </tr>
  <tr>
  <td>111111</td>
  <td>ff/55</td>
  <td>qwe1234,rto1235,yhn1238</td>
  </tr>
  <tr>
  <td>111111</td>
  <td>ee/59</td>
  <td>qwe1234,rto1235,yhn1238</td>
  </tr>
  <tr>
  <td>111111</td>
  <td>ww/55</td>
  <td>qwe1234,rto1235,yhn1238</td>
  </tr>
  <tr>
  <td>222222</td>
  <td>dd/58</td>
  <td>jkl1234,fgh1234</td>
  </tr>
  <tr>
  <td>222222</td>
  <td>null</td>
  <td></td>
  </tr>
  <tr>
  <td>333333</td>
  <td>null</td>
  <td>asz1235</td>
  </tr>
  <tr>
  <td>444444</td>
  <td>ff/55</td>
  <td>edc1234</td>
  </tr>
  <tr>
  <td>444444</td>
  <td>tt/58</td>
  <td>qaz1234</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>Need to manupulated batchNum row with comma separated based polNum and not empty boxNo and put same row. But if any condition boxNo is empty or null batchNum will put same polNum row.
  I was tried using stuff approach
  result still goes wrong.</p>
  <p>SELECT DISTINCT polNum, boxNo ,
  STUFF((SELECT DISTINCT ','+batchNum FROM #DOC a WHERE a.polNum=d.polNum or a.boxNo = d.boxNo FOR XML PATH('')),1,1,'') batchNum
  FROM #DOC d</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>polNum</th>
  <th>boxNo</th>
  <th>batchNum</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>111111</td>
  <td>null</td>
  <td>qwe1234,rto1235,yhn1238</td>
  </tr>
  <tr>
  <td>111111</td>
  <td>ff/55</td>
  <td>qwe1234,rto1235,yhn1238</td>
  </tr>
  <tr>
  <td>111111</td>
  <td>ee/59</td>
  <td>qwe1234,rto1235,yhn1238</td>
  </tr>
  <tr>
  <td>111111</td>
  <td>ww/55</td>
  <td>qwe1234,rto1235,yhn1238</td>
  </tr>
  <tr>
  <td>222222</td>
  <td>dd/58</td>
  <td>jkl1234,fgh1234</td>
  </tr>
  <tr>
  <td>222222</td>
  <td>null</td>
  <td>jkl1234,fgh1234</td>
  </tr>
  <tr>
  <td>333333</td>
  <td>null</td>
  <td>asz1235</td>
  </tr>
  <tr>
  <td>444444</td>
  <td>ff/55</td>
  <td>edc1234</td>
  </tr>
  <tr>
  <td>444444</td>
  <td>tt/58</td>
  <td>qaz1234</td>
  </tr>
  </tbody>
  </table>
  </div>