I have the following tables:
customers
customer_id name
----------------
1 bob
2 alice
3 tim
purchases
id customer_id item_bought
--------------------------
1 1 hat
2 1 shoes
3 2 glasses
3 2 glasses
4 2 book
5 3 shoes
6 1 hat
And I want to following result:
customer_name item_bought_most_often
------------------------------------
bob hat
alice glasses
tim shoes
I would do this like this (not actually tried, just the idea):
SELECT customer.name as customer_name,
MODE(item_bought) as item_bought_most_ofen
FROM customers
INNER JOIN purchases USING (customer_id)
GROUP_BY customer_id
However, the MODE aggregation function doesn't exist in Redshift.
It seems that Redshift user defined functions are just regular scalar functions, not aggregations functions. So I don't think I can define it myself.
Any workaround ?