postgresql7.1 select count(*) as number_clicks, to_char(l.click_time,'YYYY-MM-DD') as day, p.category_id from mail_link_clicks l, category_object_map cm, categories c, categories p where l.link_id = :link_id and cm.object_id = l.user_id and cm.category_id = c.category_id and c.tree_id = :tree_id and c.parent_id = p.category_id group by day, p.category_id order by p.category_id, day