In lesson 9, why does item 1) use " case order_items.name", and item 2) just use “case name”?
can I see your code? I’m having trouble with part 1 of 9.
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;
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?
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;
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;
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!