title: MySQL - How to calculate the product of multiple rows of one column
body: <p>I have the following table:</p>
  
  <pre><code>select * from bestelldetails WHERE `Bestell-Nr` = 10870;
  +------------+------------+-------------+--------+--------+
  | Bestell-Nr | Artikel-Nr | Einzelpreis | Anzahl | Rabatt |
  +------------+------------+-------------+--------+--------+
  |      10870 |         35 |          18 |      3 |      0 |
  |      10870 |         51 |          53 |      2 |      0 |
  +------------+------------+-------------+--------+--------+
  </code></pre>
  
  <p>Now, first I calculated the product of each rows "Einzelpreis (=price per piece)" and "Anzahl (pieces)" like this:</p>
  
  <pre><code>select *, Einzelpreis * Anzahl AS Rechnungssumme from bestelldetails WHERE `Bestell-Nr` = 10870;
  +------------+------------+-------------+--------+--------+----------------+
  | Bestell-Nr | Artikel-Nr | Einzelpreis | Anzahl | Rabatt | Rechnungssumme |
  +------------+------------+-------------+--------+--------+----------------+
  |      10870 |         35 |          18 |      3 |      0 |             54 |
  |      10870 |         51 |          53 |      2 |      0 |            106 |
  +------------+------------+-------------+--------+--------+----------------+
  </code></pre>
  
  <p>But now I want the SUM of each set of Data of "Bestell-Nr". I tried like this:</p>
  
  <pre><code>select *, Einzelpreis * Anzahl AS Rechnungssumme from bestelldetails WHERE `Bestell-Nr` = 10870 GROUP BY `Bestell-Nr`;
  +------------+------------+-------------+--------+--------+----------------+
  | Bestell-Nr | Artikel-Nr | Einzelpreis | Anzahl | Rabatt | Rechnungssumme |
  +------------+------------+-------------+--------+--------+----------------+
  |      10870 |         35 |          18 |      3 |      0 |             54 |
  +------------+------------+-------------+--------+--------+----------------+
  </code></pre>
  
  <p>But this obviously doesnt work. Inside "Rechnungssumme", only the product of ONE record is displayed, not the sum of all the values aggregated from each set of data. </p>
  
  <p>In my actual usecase, there will be tens of thousands of different orders, all with their own "Bestell-Nr". For each of these orders, this operation must create aggregated rows where the SUM of the products of each row must be calculated and correctly displayed.</p>