title: How to us Group By in SQL with a JOIN
body: <p>I am wondering if anyone could help me. I trying to write a query which will group all the order detail lines to each product.</p>
  
  <pre><code>    SELECT
        Line.NetAmount, Line.QtyDespatch, Line.QtyOrder,
        Line.Price, Line.Price * Line.QtySent AS 'Value'
        FROM  dbo.orderdetails line
  
     LEFT JOIN
     (
      SELECT  products.ProductID, products.CompanyID AS StockCompanyID,  
      products.StockCode  FROM dbo.products
     ) 
     Stock ON Line.ProductID = products.ProductID AND products.StockCompanyID = Line.CompanyID    
  
     WHERE Line.CompanyID = 1
     ORDER BY Stock.StockID ASC
  </code></pre>
  
  <p>The results I am getting are for each order details line individually but I want to group-by the each product id and have (Line.Price * Line.QtySent) as sum for each product and show each product once.</p>
  
  <p>The result I am getting are</p>
  
  <pre><code>PID     Net sent   qty      Price  Value
  39044   12  0       5       2.4     0
  39044   12  0       5       2.4     0
  39044   12  0       5       2.4     0
  39044   12  0       5       2.4     0
  </code></pre>
  
  <p>But I do not want to get it for each line but a cumulative value for all line by product. Basically cumulative  totals for each product</p>