title: SQL: Grouping on a subquery
body: <p>I have a query where I want to aggregate column <code>ElementAantal</code>. However this is the first time I am grouping on columns where there is a subquery <code>ArtikelInhoud</code> involved. </p>
  
  <p>Please your advise on how to make the query work.</p>
  
  <pre><code>SELECT          
                  pl.[Location Code] AS Locatie,
                  pl.[Ending Date] AS ProdOrderDatum,
                  pl.[Item No_] AS Artikelnr,
                  pl.Description AS Artikel,
                  pbl.No_ AS Elementnr,
                  pbl.Description AS Element,
                  SUM(pbl.Quantity * pl.Quantity) AS ElementAantal,   -----&gt; Aggregating this column
                  i.[Item Category Code] AS ElementCategorie,
                  (
                      SELECT      Value
                      FROM        [$Attribute Specification] att
                      WHERE       [Table ID] = 27 AND Code = 'INH' AND att.[Unique Record Identifier] = i1.[Unique Record Identifier]
                  ) AS ArtikelInhoud,
                  i.[Gross Weight] AS ElementGewicht,
                  v.Name AS Leverancier
  FROM            [$Prod_ Order Line] pl
  LEFT JOIN       [$Production BOM Line] pbl ON pl.[Production BOM No_] = pbl.[Production BOM No_]
  LEFT JOIN       [$Item] i ON pbl.No_ = i.No_
  LEFT JOIN       [$Item] i1 ON pl.[Item No_] = i1.No_
  LEFT JOIN       [$Vendor] v ON i.[Vendor No_] = v.No_
  GROUP BY        pl.[Location Code],
                  pl.[Ending Date],
                  pl.[Item No_],
                  pl.Description,
                  pbl.No_,
                  pbl.Description,
                  i.[Item Category Code],
                  (
                      SELECT      Value
                      FROM        [$Attribute Specification] att
                      WHERE       [Table ID] = 27 AND Code = 'INH' AND att.[Unique Record Identifier] = i1.[Unique Record Identifier]
                  ),
                  i.[Gross Weight],
                  v.Name 
  </code></pre>