I have 3 tables in my DB (MySQL).
categories (name:string) items (name:string, category_id:int) votes (value:int, item_id:int, created_at:datetime) >
categories (name:string) items (name:string, category_id:int) votes (value:int, item_id:int, created_at:datetime)
SELECT c.name, sum(v.value) as cnt FROM categories c JOIN items i ON i.category_id = c.id JOIN votes v ON v.item_id = i.id WHERE v.created_at > #{1.week.ago} GROUP BY c.name ORDER BY cnt DESC LIMIT 5;
Edit: good point andrew, I fixed the query