title: Error : Windowed functions cannot be used in the context of another windowed function or aggregate
body: <p>I am trying to get the total amount for each invoice using below query, but I got the following error: </p>
  
  <blockquote>
    <p>Msg 4109, Level 15, State 1, Line 3
    Windowed functions cannot be used in the context of another windowed function or aggregate.</p>
  </blockquote>
  
  <p><strong>Query</strong></p>
  
  <pre><code>SELECT
    SP.InvoiceNo,
    Sum(CAST((SPD.Quantity * SPD.UnitPrice) / (SUM(SPD.Quantity * SPD.UnitPrice) OVER ()) * Sp.Shipping_Cost + (SPD.Quantity * SPD.UnitPrice) / (SUM(SPD.Quantity * SPD.UnitPrice) OVER ()) * Sp.Customs_Cost + SPD.Quantity * SPD.UnitPrice - (SPD.Discount / 100) * SPD.Quantity * SPD.UnitPrice AS decimal(10, 2))) AS [Total Amount]
  FROM dbo.Stock_Purchase SP
  INNER JOIN dbo.Stock_Purchase_Details SPD
    ON SP.Purchase_ID = SPD.Purchase_ID
  INNER JOIN dbo.Store S
    ON SPD.Pro_ID = S.Pro_ID
    group by SP.InvoiceNo,SPD.Quantity,SPD.UnitPrice
  </code></pre>