7. Product Sum 2


#1

I'm constantly getting the following error message "Error: near line 1: near ")": syntax error ".

Here is my query

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

What am I doing wrong?


#2

Your parentheses are off. Your subquery should read as:
(select sum(amount_paid) from order_items) * 100.0, 2) as pct


#3

great it worked for me


#4

here some more things. i wonder that are we dividing the same thing to same thins sum(amount_paid) : sum(amount_paid)
i am a bit confused. Help


#5

I was confused at first too, but we are not dividing a numerator by an identical denominator. Simplified, the numerator is (name, sum) while the denominator is (sum). The denominator is the subquery that returns sum of all amounts. The numerator is the query of sum amounts by name.


#6

Actually, what makes the numerator be the sum of only the values of a specific food is the use of GROUP BY, not the usage of "name" in the first SELECT. The part of "name" is just calling the columns that are going to be shown. You could obtain the exact same result without actually using "name" in the SELECT.

The numerator is the SUM of the values GROUPED BY name. The GROUP BY at the end is still part of the calculation, if you change it to GROUP BY id, for example, you get the percentage that represents a single item by itself.

The denominator is the subquery that is in parenthesis and is only the SUM of all.


#7

This is my code:

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

I can'y figure out what is wrong with it. I keep getting the error message "Error: near line 1"


#9

gilli i have the same problem


#10

this is the closest i got to any answer:
select name, round(sum(amount_paid) /
(select amount_paid from order_items) / 100.0 , 2) as pct
from order_items
group by 1
order by 2 desc;

however the percentages add to more than 100%, and using *100.0 gives four-digit numbers for each result. furthermore, it still tells me to use a subquery. any ideas?


#11

never mind, here's the answer:

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

:smiley:


#12

credit: https://discuss.codecademy.com/t/7-product-sum-2-placement-issues/20376?u=asitnik

:slight_smile:


#13

Thank you, this cleared up my confusion. I forgot that “GROUP BY”.