Grouping with case statement Part 2, maybe a mistake?


#1

Hi i can't get the second part of the exercise cause each time i'll try something it makes hanging my browser.
Maybe i'm putting the bad answer in place of /**/


9.2 What is wrong here?
#2

With that code i must restart firefox :

select *,
  case name
    when 'kale-smoothie'    then 'smoothie'
    when 'banana-smoothie'  then 'smoothie'
    when 'orange-juice'     then 'drink'
    when 'soda'             then 'drink'
    when 'blt'              then 'sandwich'
    when 'grilled-cheese'   then 'sandwich'
    when 'tikka-masala'     then 'dinner'
    when 'chicken-parm'     then 'dinner'
     else 'other'
  end as category, round(1.0 * sum(amount_paid) /
    (select amount_paid from order_items) * 100, 2) as pct
from order_items
group by 1
order by 2 desc;

#3

Okay, so you have to take out the " *, "
in part 1 you were showing both * (all columns) and the new column "category"
in part 2 you are only showing the new column category.

Not sure why it doesn't work though your way....

also, you get the wrong answer if you use "select amount_paid from order_items"
It should be "select SUM(amount_paid) from order_items)


#4

Thanks it worked now, i think my biggest problem was that i was selecting everything instead of one column


#5

I am also having a problem with this section and keep getting this error:

Error: near line 1: near ";": syntax error Error: near line 14: near "(": syntax error

This is the query I am running:

select
case name
when 'kale-smoothie' then 'smoothie'
when 'banana-smoothie' then 'smoothie'
when 'orange-juice' then 'drink'
when 'soda' then 'drink'
when 'blt' then 'sandwich'
when 'grilled-cheese' then 'sandwich'
when 'tikka-masala' then 'dinner'
when 'chicken-parm' then 'dinner'
else 'other'
end as category, round(1.0 * 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;


#6

Hi, don't worry, I have sorted this by deleting and retyping the query exactly the same and it has run without errors


#7

Do this

select case name
when 'kale-smoothie' then 'smoothie'
when 'banana-smoothie' then 'smoothie'
when 'orange-juice' then 'drink'
when 'soda' then 'drink'
when 'blt' then 'sandwich'
when 'grilled-cheese' then 'sandwich'
when 'tikka-masala' then 'dinner'
when 'chicken-parm' then 'dinner'
else 'other'
end as category, round(1.0 * sum(amount_paid) /
(select sum(amount_paid) from order_items) * 100, 2) as pct
from order_items
group by 1
order by 2 desc;


#8

Thanks, this resolved my problems as well, where I had focused on changing the select (sum(amount_paid)...) statement but completely ignored the select *, statement which started the query and was left from an earlier part of the exercise.


#10