SELECT to_char(date_trunc('day'::text, COALESCE(trial_date, tour_date)), 'YYYY-MM-DD'::text) AS appt_date,
branch,
visit_category,
appt_category,
contact_owner,
assigned_pic,
count(*) AS mql,
count("mqlPlus_date") AS mqlplus,
count("tourPlus_date") AS tourplus,
count("trialPlus_date") AS trialplus
FROM appt
GROUP BY (to_char(date_trunc('day'::text, COALESCE(trial_date, tour_date)), 'YYYY-MM-DD'::text)), branch, visit_category, appt_category, contact_owner, assigned_pic
ORDER BY (to_char(date_trunc('day'::text, COALESCE(trial_date, tour_date)), 'YYYY-MM-DD'::text));