There is a table like this, how can I get customer_id
, amount_spent
, top_item
out of it?
amount_spent
the amount spent on all items by this customer
top_item
, which displays the name of the item for which the customer has spent the most money
I have tried the following query, however I cannot output the top_1 item with it
select customer_id, sum(item_number * item_price) as amount_spent_1m
from temp
group by customer_id
Check the demo here.
2
Answers
You can achieve it as below :
It gives me the following result :
I am sure there is a way to do this with one less step but my brain is not seeing it right now –
db<>fiddle