create or replace view v_visits as
select
    v1.user_id,
    users.name as user_name,
    (select count(id) from visits v2 where v2.status = 2 and v2.user_id = v1.user_id ) as scheduled_visits,
    (select count(id) from visits v3 where v3.status = 3 and v3.user_id = v1.user_id ) as completed_visits
from visits v1
join users on users.id = v1.user_id
group by v1.user_id;