Lesson 9, order_items.name vs name


#1

In lesson 9, why does item 1) use " case order_items.name", and item 2) just use "case name"?


#2

can I see your code? I'm having trouble with part 1 of 9.


#3

After seeing this I tried modifying to see if this was why it wouldn't accept anything (even the suggested answer given after failing so many times). This is what I think the code should be; did you ever get this to work?

select
case order_items.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 category
order by pct;


#4

I have the same problem. I dont know how to solve it.
In part 9/12 (Grouping with Case Statement) I wrote:
select
case order_items.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;

and got feedback "Add a category column.".

Anybody can help?


#5

OK, I get the right answer for 9/12 topic.

select *,
  case order_items.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
from order_items
order by id
limit 100;

2.

select
  case order_items.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;

#6

Have a question regaring the following portion of code…

“end as category, round(1.0 * sum(amount_paid)/(select SUM(amount_paid) from order_items)*100,2) as pct
from order_items”

Why do you need the comma after “category”? Not sure i understand the important and why I couldn’t leave this out. thanks!