I have table where user daily activities are saved, and now I have to present these activities in report in following form.
I have written a query,
select ua.UserID, count(ua.UserID),
(select count(*) form user_activities t1 where t1.UserID = ua.UserID and ActivityID = 1 and t1.Date = ua.Date group by t1.UserID, t1.ActivityID, t1.Date) as Meeting,
(select count(*) form user_activities t1 where t1.UserID = ua.UserID and ActivityID = 2 and t1.Date = ua.Date group by t1.UserID, t1.ActivityID, t1.Date) as Training,
ua.Date
from user_activities ua
group by ua.UserID, ua.ActivityID, ua.Date
But I know it is not an efficient one and activity IDs are hardcoded and in future there can be new type of activities.
Can you guide me to make it more dynamic and efficient?
Thanks