Report. Collect statistics about the quarter

Task:


RT-2103

Query:

select bu.name,
       concat(consultants.first_name, ' ', consultants.last_name) as consultant_name,
       COUNT(distinct case
                          when table_name = 'candidates_vacancy_requests' and
                               field_name = 'status' and
                               old_value = '1'
                              then cvr.id end) as created,
       COUNT(distinct case
                          when table_name = 'candidates_vacancy_requests' and
                               field_name = 'status' and
                               new_value = '6'
                          then cvr.id end) as shared,
       COUNT(distinct case
                          when table_name = 'candidates_vacancy_requests' and
                               field_name = 'status' and
                               new_value = '13'
                              then cvr.id end) as rocked,
       COUNT(distinct case
                          when table_name = 'candidates' and
                               field_name = 'status_id' and
                               new_value = '5'
                              then candidates.id end) as active
from arhistory ar
         left join candidates_vacancy_requests cvr on ar.row_id = cvr.id
         left join candidates on ar.row_id = candidates.id
         left join consultants
                   on case
                          when table_name = 'candidates_vacancy_requests' then cvr.consultants_id = consultants.id
                          else candidates.consultants_id = consultants.id
                       end
         left join business_units bu on bu.id = consultants.business_unit_id
where date(to_timestamp(ar.created_at)) between '2023-01-01' and '2023-03-31'
group by bu.name, consultant_name
order by bu.name;

Comments

Leave a Reply