0

I'm taking up the SQL section of the Google Analytics course and I was thinking of ways of optimizing the code from one of the lessons.

Can anyone please explain to me why I'm getting an unrecognized name error in the WHEN statement for number_of_orders? I've already aliased it earlier in the query.

SELECT
  Warehouse.warehouse_id,
  CONCAT(Warehouse.state, ': ', Warehouse.warehouse_alias) AS warehouse_name,
  COUNT(Orders.order_id) AS number_of_orders,
  (SELECT
    COUNT(*)
    FROM warehouse_orders.Orders
  ) AS total_orders,
  CASE
    WHEN number_of_orders / total_orders <= 0.20 THEN "Fulfilled 0-20% of the orders"
    WHEN number_of_orders / total_orders > 0.20 AND number_of_orders/total_orders <= 0.60 THEN "Fulfilled 21-60% of the orders"
    ELSE "Fulfilled more than 60% of the orders"
  END AS fulfillment_summary
FROM warehouse_orders.Warehouse AS Warehouse
LEFT JOIN warehouse_orders.Orders AS Orders
ON Orders.warehouse_id = Warehouse.warehouse_id
GROUP BY
  Warehouse.warehouse_id,
  warehouse_name
HAVING
  COUNT(Orders.orders) > 0;
Den Lim
  • 19
  • 3

0 Answers0