This is in SQL Server...but easy to convert for a MySQL guy I would think!
select top 5
c.name as CategoryName, sum(v.value) as VoteSum
from categories c inner join items i
on c.category_id = i.category_id
inner join votes v
on i.item_id = v.item_id
where created_at between dateadd(week,-1,getdate()) and getdate()
group by c.name
order by sum(v.value) desc
select top 5 --number of categories to show in calculation
c.name as CategoryName, sum(v.value) as VoteSum --get the categoryname and the sum of votes
from categories c inner join items i --join the heirarchy: categories to items
on c.category_id = i.category_id --on category_id
inner join votes v --items to votes
on i.item_id = v.item_id --on item_id
where created_at between dateadd(week,-1,getdate()) and getdate() --specify the date range to include
group by c.name --group the results by the category
order by sum(v.value) desc --order the results by the summed value
Here is some sql (from SQL Server) to get the tables up and running:
CREATE TABLE [categories](
[category_id] [int] IDENTITY(1,1) NOT NULL,
[name] [varchar](50) NOT NULL
)
CREATE TABLE [items](
[item_id] [int] IDENTITY(1,1) NOT NULL,
[category_id] [int] NOT NULL,
[name] [varchar](50)
)
CREATE TABLE [dbo].[votes](
[vote_id] [int] IDENTITY(1,1) NOT NULL,
[value] [int] NOT NULL,
[item_id] [int] NOT NULL,
[created_at] [datetime] NOT NULL
)
insert into categories (name) values (' asp.net ')
insert into categories (name) values (' c# ')
insert into categories (name) values (' vb ')
insert into categories (name) values (' sql ')
insert into categories (name) values (' html ')
insert into categories (name) values (' javascript ')
insert into items (category_id, name) values ( 1 ,' session handling ')
insert into items (category_id, name) values ( 1 ,' mvc vs mvp ')
insert into items (category_id, name) values ( 1 ,' code behind or no code behind ')
insert into items (category_id, name) values ( 2 ,' LINQ? ')
insert into items (category_id, name) values ( 2 ,' lamdas ')
insert into items (category_id, name) values ( 2 ,' multi-threaded code ')
insert into items (category_id, name) values ( 2 ,' SOLID principles ')
insert into items (category_id, name) values ( 3 ,' vb vs C# ')
insert into items (category_id, name) values ( 3 ,' VB.NET over vb6 ')
insert into items (category_id, name) values ( 4 ,' CLR procedures or stored procedures ')
insert into items (category_id, name) values ( 4 ,' ORMs vs stored procedures and views ')
insert into items (category_id, name) values ( 6 ,' jquery instead of standard DOM ')
insert into votes (value, item_id, created_at) values ( -1 , 1 ,' 6/26/2009 18:59 ')
insert into votes (value, item_id, created_at) values ( 1 , 1 ,' 6/26/2009 18:59 ')
insert into votes (value, item_id, created_at) values ( 3 , 1 ,' 6/26/2009 18:59 ')
insert into votes (value, item_id, created_at) values ( 3 , 1 ,' 6/26/2009 18:59 ')
insert into votes (value, item_id, created_at) values ( 2 , 2 ,' 6/26/2009 18:59 ')
insert into votes (value, item_id, created_at) values ( 2 , 2 ,' 6/26/2009 18:59 ')
insert into votes (value, item_id, created_at) values ( 4 , 2 ,' 6/26/2009 18:59 ')
insert into votes (value, item_id, created_at) values ( -3 , 2 ,' 6/26/2009 18:59 ')
insert into votes (value, item_id, created_at) values ( 2 , 4 ,' 6/26/2009 19:00 ')
insert into votes (value, item_id, created_at) values ( 6 , 4 ,' 6/26/2009 19:00 ')
insert into votes (value, item_id, created_at) values ( 3 , 4 ,' 6/26/2009 19:00 ')
insert into votes (value, item_id, created_at) values ( 5 , 4 ,' 6/26/2009 19:00 ')
insert into votes (value, item_id, created_at) values ( 8 , 7 ,' 6/26/2009 19:00 ')
insert into votes (value, item_id, created_at) values ( 3 , 6 ,' 6/26/2009 19:00 ')
insert into votes (value, item_id, created_at) values ( 8 , 7 ,' 6/26/2009 19:01 ')
insert into votes (value, item_id, created_at) values ( 2 , 5 ,' 6/26/2009 19:01 ')