Advanced Aggregates

Grouping with Case Statements

To see if our smoothie suspicion has merit, let's look at purchases by category. We can group the order items by what type of food they are, and go from there. Since our order_items table does not include categories already, we'll need to make some!

Previously we've been using group by with a column (like order_items.name) or a function (like date(orders.ordered_at)).

We can also use group by with expressions. In this case a case statement is just what we need to build our own categories. case statements are similar to if/else in other languages.

Here's the basic structure of a case statement:

case {condition}
  when {value1} then {result1}
  when {value2} then {result2}
  else {result3}
Community Forums
Get help and ask questions in the Codecademy Forums
Report a Bug
If you see a bug or any other issue with this page, please report it here.