Hello,
I am having difficulty understanding why the GROUP BY 1 line is necessary here
SELECT
c.first_name || " " || c.last_name customer_name,
COUNT(i.invoice_id) number_of_purchases,
SUM(i.total) total_spent,
CASE
WHEN sum(i.total) < 40 THEN 'small spender'
WHEN sum(i.total) > 100 THEN 'big spender'
ELSE 'regular'
END
AS customer_category
FROM invoice i
INNER JOIN customer c ON i.customer_id = c.customer_id
GROUP BY 1
ORDER BY 1
My initial code did not contain the ‘GROUP BY 1’ and the output gives one single customer:
customer_name: Phil Hughes
number_of_purchases: 614
total_spent: 4709.429999999994
customer_category: big spender
Inputting the correct code and locating this customer I see the values are different:
customer_name: Phil Hughes
number_of_purchases: 11
total_spent: 98.01
customer_category: regular
Why is ‘group by 1’ important here when the instructions specifically states we do not have any 2 customers with the same name?