That's why the second sum_amount) paid is in a nested select statement,
Looking at the SQL, what I see is:
- You are selecting the name and then grouping by the name (group by 1)
- Then when you select the first round(sum(amount_paid)) it will be the total of amount paid by that group.
- Then you divide that by (select sum(amount_paid) from order_items). This time you're selecting the total sum of all order_items amount paid it's independent of the group by because it's in a nested statement.
so let's say you have 10.00 $ in totals sales for the name "smoothies". But you have 500 dollars in total sales.
What this SQL does is select all smoothies, sum them up then divide by the sum of all total sales from order_items. Finally it multiplies times 100.
I hope that makes sense, I tried to explain it in different ways as different people take in information differently.