title: How to place subquery in SQL Server 2008
body: <p>I have a main SQL query which returns stock quantity in dates between and I also need to return the stock as on date along with between dates result.</p>
  
  <p>Main query:</p>
  
  <pre><code>SELECT 
      TT.TranRId, MAX(TT.TInQty) AS InQty, MAX(TT.TOutQty) AS OutQty
  FROM
      TReg TR, TTrans TT
  WHERE 
      TR.TRegId = TT.TrRegId
      AND TT.Stid = 2
      AND TR.TransDate BETWEEN '2018-08-25' AND '2018-08-28'
  GROUP BY 
      TT.TranRId 
  ORDER BY 
      TT.TranRId
  </code></pre>
  
  <p>Sub query:</p>
  
  <pre><code>(SELECT TT.TransRId, (SUM(TT.TInQty) - SUM(TT.TOutQty)) 
  FROM TTrans TT, TReg TR
  WHERE TR.TransDate &lt;= '2018-08-24' 
    AND TR.TRegId = TT.TrRegId
    AND TT.Stid = 2 GROUP BY TT.TranRId) --AS Stock
  </code></pre>
  
  <p>Please help where I should include my sub query in my main query </p>
  
  <p>To get output as follows:</p>
  
  <pre><code>TransRId    Stock   InQty   OutQty
  ----------------------------------
  41          700     1       1000
  42          800     5       500
  </code></pre>