select tree_id from ml_country_category_tree where package_id = :mailing_list_package_id select count(*) as number_clicks, to_char(l.click_time,'J') as cur_date, p.category_id from mail_link_clicks l, category_object_map cm, categories c, categories p where l.link_id = :link_id and l.mail_id = :mail_job_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 cur_date, p.category_id order by cur_date, p.category_id SELECT category_id as cur_category_id FROM categories WHERE tree_id = :tree_id AND parent_id is null