# #6 Meal Sums - I don't understand what we're doing

#1

`select name, round(sum(amount_paid) / (select sum(amount_paid) from order_items) * 100.0, 2) as pctfrom order_items group by 1 order by 2 desc;`

It looks like we're diving the sum of the amount_paid column by itself. I don't get what we're doing here.

#2

That's why the second sum_amount) paid is in a nested select statement,
Looking at the SQL, what I see is:

1. You are selecting the name and then grouping by the name (group by 1)
2. Then when you select the first round(sum(amount_paid)) it will be the total of amount paid by that group.
3. 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.

#3

I am getting the following error
Error: near line 1: near ";": syntax error Error: near line 3: near "(": syntax error

#4

So the first sum(amount_paid) is summing up by groups, and the second sum(amount_paid) is the sum of the whole thing not grouping?

#5

Yes @brendan_whiting,

Essentially in the second select column your taking the sum of all amount_paid, then grouping it by name. Finally you're diving it by the sum of all (amount_paid) and multiplying it by 100.

This is why you have another select statement nested inside the select statement