title: Concatenating Multiple Rows Over Same ID in SQL
body: <p>I'm trying to concatenate multiple rows on the basis of same <code>OrderID</code> for a particular person. The goal is to achieve a row that contains the total order of each specific <code>OrderID</code>.</p>
  
  <p>I have a data from a query as follows:</p>
  
  <p><a href="https://i.stack.imgur.com/ux3D3.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ux3D3.png" alt="enter image description here"></a>
  What I need is to concatenate the particulars on basis of same <code>OrderID</code>.</p>
  
  <p>Result should be:</p>
  
  <pre><code>PartyName   | partyCode | OrderID | particulars                                  | Price | Total
  
  Fine Mark   | 3         | 5069    | suji50kg/1500/2,maida50kg/1/2,suji50kg/1500/3| 7802  | 19432
  </code></pre>
  
  <p>What I have done:</p>
  
  <pre><code>SELECT 
      @col = COALESCE(@col, ' ') + ' , ' + particulars 
  FROM (
      SELECT 
          a.orderID,
          CONCAT(a.item, '/', a.unitPrice, '/', a.quantity) as particulars 
      FROM tblOrderDetails a, tblAddParty b, tblPartyOrder c 
      WHERE a.orderID = c.orderID 
          AND b.partyID = c.partyID 
          AND b.partyName = 'Fine Marketing Islamabad'
          AND c.date &gt;= '2014-03-06' 
          AND c.date &lt;= '2016-03-25') mine
  
  SELECT 
      date, 
      partyName, 
      partyCode, 
      orderID, 
      @col as Particulars, 
      MIN(CASE WHEN total &gt; credit THEN price ELSE 0 END) AS debit,
      MIN(CASE WHEN total &lt; credit THEN price ELSE 0 END) AS credit,
      MIN(credit) + SUM(totalprice) AS balance 
  FROM (
      SELECT 
          c.date, 
          b.partyName, 
          b.partyCode,
          a.orderID, 
          CONCAT(a.item, '/', a.unitPrice, '/', a.quantity) AS particulars,
          a.totalprice,  
          c.credit, 
          c.total,
          c.price 
      FROM tblOrderDetails a, tblAddParty b, tblPartyOrder c 
      WHERE a.orderID = c.orderID  
          AND b.partyID = c.partyID 
          AND b.partyName = 'Fine Marketing Islamabad' 
          AND c.date &gt;= '2014-03-06' 
          AND c.date &lt;= '2016-03-25') marked 
  GROUP BY 
      partyName, 
      partyCode, 
      orderID, 
      date 
  ORDER BY date
  </code></pre>
  
  <p>The result:</p>
  
  <p><a href="https://i.stack.imgur.com/HCWzw.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/HCWzw.png" alt="enter image description here"></a></p>