Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- select
- rc.name as KS,
- rb.name as branch_name,
- cl.user_id ,
- ru.login ,
- cl.schedule_date,
- count(cl.id) as activity_per_day,
- coalesce(max(cap.total_adhoc),0) as total_adhoc,
- coalesce(max(cap.call_adhoc),0) as call_adhoc,
- coalesce(max(cap.visit_adhoc),0) as visit_adhoc,
- -- EXTRACT(DOW FROM cl.schedule_date) AS day_of_week_number,
- -- CASE
- -- WHEN EXTRACT(DOW FROM cl.schedule_date) = 0 THEN 'Sunday'
- -- WHEN EXTRACT(DOW FROM cl.schedule_date) = 1 THEN 'Monday'
- -- WHEN EXTRACT(DOW FROM cl.schedule_date) = 2 THEN 'Tuesday'
- -- WHEN EXTRACT(DOW FROM cl.schedule_date) = 3 THEN 'Wednesday'
- -- WHEN EXTRACT(DOW FROM cl.schedule_date) = 4 THEN 'Thursday'
- -- WHEN EXTRACT(DOW FROM cl.schedule_date) = 5 THEN 'Friday'
- -- WHEN EXTRACT(DOW FROM cl.schedule_date) = 6 THEN 'Saturday'
- -- END AS day_of_week_name
- case when count(cl.id) - max(cap.total_adhoc) > 0 then count(cl.id) - max(cap.total_adhoc) else count(cl.id) end as system_generated,
- coalesce(max(cap.customer_adhoc),0) as customer_adhoc,
- coalesce(max(cap.leads_adhoc),0) as leads_adhoc,
- SUM(CASE WHEN cl.status_crm_pipeline = 'close_and_complete' THEN 1 ELSE 0 END) as complete,
- SUM(CASE WHEN cl.status_crm_pipeline != 'close_and_complete' THEN 1 ELSE 0 END) as incomplete,
- SUM(CASE WHEN cl.status_meet_visit = 'meet_customer' THEN 1 ELSE 0 END) as meet,
- SUM(CASE WHEN cl.status_meet_visit != 'meet_customer' THEN 1 ELSE 0 END) as not_meet
- from crm_lead cl
- left join (
- select
- cap2.schedule_date,
- cap2.user_id,
- count(cap2.id) as total_adhoc,
- SUM(CASE WHEN cl2.type_pipeline like '%call%' THEN 1 ELSE 0 END) as call_adhoc,
- SUM(CASE WHEN cl2.type_pipeline like '%visit%' THEN 1 ELSE 0 END) as visit_adhoc,
- SUM(CASE WHEN cap2.type_pipeline = 'visit_plan_customer' THEN 1 ELSE 0 END) as customer_adhoc,
- SUM(CASE WHEN cap2.type_pipeline = 'visit_plan_leads' THEN 1 ELSE 0 END) as leads_adhoc
- from crm_adhoc_plan cap2
- left join crm_lead cl2 on cap2.pipeline_id = cl2.id
- where cap2.type_pipeline in ('visit_plan_customer','visit_plan_leads')
- group by
- cap2.schedule_date,
- cap2.user_id) cap on cl.user_id = cap.user_id and cl.schedule_date = cap.schedule_date
- left join res_users ru on cl.user_id = ru.id
- left join res_partner rp on cl.partner_id = rp.id
- left join crm_lead_pool clp ON cl.crm_lead_pool_id = clp.id
- left join res_branch rb on ru.branch_id = rb.id
- left join master_company_mnf rc on rb.company_id = rc.id
- --where
- where cl.schedule_date between '2023-08-01' and '2023-08-16'
- --and cl.user_id = 6
- group by cl.user_id , cl.schedule_date, ru.login, rc.name, rb.name
- order by cl.user_id ,cl.schedule_date asc
- ---------------------------------------------------------------------------------------------------
- --adhoc hunter
- select
- rc.name as KS,
- rb.name as branch_name,
- ru.login ,
- cap.user_id ,
- cap.schedule_date ,
- count(cl_customer.id) as adhoc_customer,
- count(cl_lead.id) as adhoc_lead,
- count(cl_customer.id) + count(cl_lead.id) as total_adhoc,
- SUM(CASE WHEN cl_customer.status_crm_pipeline = 'close_and_complete' THEN 1 ELSE 0 END) as complete_customer,
- SUM(CASE WHEN cl_customer.status_crm_pipeline != 'close_and_complete' THEN 1 ELSE 0 END) as incomplete_customer,
- SUM(CASE WHEN cl_lead.status_crm_pipeline = 'close_and_complete' THEN 1 ELSE 0 END) as complete_lead,
- SUM(CASE WHEN cl_lead.status_crm_pipeline != 'close_and_complete' THEN 1 ELSE 0 END) as incomplete_lead,
- SUM(CASE WHEN cl_customer.status_meet_visit = 'meet_customer' THEN 1 ELSE 0 END) as meet_customer,
- SUM(CASE WHEN cl_customer.status_meet_visit != 'meet_customer' THEN 1 ELSE 0 END) as not_meet_customer,
- SUM(CASE WHEN cl_lead.status_meet_visit = 'meet_customer' THEN 1 ELSE 0 END) as meet_lead,
- SUM(CASE WHEN cl_lead.status_meet_visit != 'meet_customer' THEN 1 ELSE 0 END) as not_meet_lead
- from crm_adhoc_plan cap
- left join crm_lead cl_customer on cap.schedule_date = cl_customer.schedule_date and cap.user_id = cl_customer.user_id and cap.partner_id = cl_customer.partner_id
- left join crm_lead cl_lead on cap.schedule_date = cl_lead.schedule_date and cap.user_id = cl_lead.user_id and cap.crm_lead_pool_id = cl_lead.crm_lead_pool_id
- left join res_users ru on cap.user_id = ru.id
- left join res_partner rp on cap.partner_id = rp.id
- left join crm_lead_pool clp ON cap.crm_lead_pool_id = clp.id
- left join res_branch rb on ru.branch_id = rb.id
- left join master_company_mnf rc on rb.company_id = rc.id
- where cap.schedule_date between '2023-08-01' and '2023-08-16' and cap.type_pipeline in ('visit_plan_customer','visit_plan_leads')
- group by cap.schedule_date, cap.user_id ,rc.name, rb.name,ru.login
- ------------------------------------------------------------
- -- farmer
- select
- rc.name as KS,
- rb.name as branch_name,
- cl.user_id ,
- ru.login ,
- cl.schedule_date,
- count(cl.id) as activity_per_day,
- coalesce(max(cap.total_adhoc),0) as total_adhoc,
- coalesce(max(cap.call_adhoc),0) as call_adhoc,
- coalesce(max(cap.visit_adhoc),0) as visit_adhoc,
- -- EXTRACT(DOW FROM cl.schedule_date) AS day_of_week_number,
- -- CASE
- -- WHEN EXTRACT(DOW FROM cl.schedule_date) = 0 THEN 'Sunday'
- -- WHEN EXTRACT(DOW FROM cl.schedule_date) = 1 THEN 'Monday'
- -- WHEN EXTRACT(DOW FROM cl.schedule_date) = 2 THEN 'Tuesday'
- -- WHEN EXTRACT(DOW FROM cl.schedule_date) = 3 THEN 'Wednesday'
- -- WHEN EXTRACT(DOW FROM cl.schedule_date) = 4 THEN 'Thursday'
- -- WHEN EXTRACT(DOW FROM cl.schedule_date) = 5 THEN 'Friday'
- -- WHEN EXTRACT(DOW FROM cl.schedule_date) = 6 THEN 'Saturday'
- -- END AS day_of_week_name
- case when count(cl.id) - max(cap.total_adhoc) > 0 then count(cl.id) - max(cap.total_adhoc) else count(cl.id) end as system_generated,
- coalesce(max(cap.customer_adhoc),0) as customer_adhoc,
- coalesce(max(cap.leads_adhoc),0) as leads_adhoc,
- SUM(CASE WHEN cl.status_crm_pipeline = 'close_and_complete' THEN 1 ELSE 0 END) as complete,
- SUM(CASE WHEN cl.status_crm_pipeline != 'close_and_complete' THEN 1 ELSE 0 END) as incomplete,
- SUM(CASE WHEN cl.status_call = 'invalid' THEN 1 ELSE 0 END) as invalid,
- SUM(CASE WHEN cl.status_call = 'not_answer' THEN 1 ELSE 0 END) as not_answer,
- SUM(CASE WHEN cl.status_call = 'answer' THEN 1 ELSE 0 END) as answer,
- SUM(CASE WHEN cl.status_call = 'chat_only' THEN 1 ELSE 0 END) as chat_only
- from crm_lead cl
- left join (
- select
- cap2.schedule_date,
- cap2.user_id,
- count(cap2.id) as total_adhoc,
- SUM(CASE WHEN cl2.type_pipeline like '%call%' THEN 1 ELSE 0 END) as call_adhoc,
- SUM(CASE WHEN cl2.type_pipeline like '%visit%' THEN 1 ELSE 0 END) as visit_adhoc,
- SUM(CASE WHEN cap2.type_pipeline = 'call_plan_customer' THEN 1 ELSE 0 END) as customer_adhoc,
- SUM(CASE WHEN cap2.type_pipeline = 'call_plan_leads' THEN 1 ELSE 0 END) as leads_adhoc
- from crm_adhoc_plan cap2
- left join crm_lead cl2 on cap2.pipeline_id = cl2.id
- where cap2.type_pipeline in ('call_plan_customer','call_plan_leads')
- group by
- cap2.schedule_date,
- cap2.user_id) cap on cl.user_id = cap.user_id and cl.schedule_date = cap.schedule_date
- left join res_users ru on cl.user_id = ru.id
- left join res_partner rp on cl.partner_id = rp.id
- left join crm_lead_pool clp ON cl.crm_lead_pool_id = clp.id
- left join res_branch rb on ru.branch_id = rb.id
- left join master_company_mnf rc on rb.company_id = rc.id
- --where
- where cl.schedule_date between '2023-08-01' and '2023-08-16'
- and cl.type_pipeline in ('call_plan_customer','call_plan_leads')
- --and cl.user_id = 6
- group by cl.user_id , cl.schedule_date, ru.login, rc.name, rb.name
- order by cl.user_id ,cl.schedule_date asc
- ---------------------------------------------------------------------------------------------------
- --adhoc farmer
- select
- rc.name as KS,
- rb.name as branch_name,
- ru.login ,
- cap.user_id ,
- cap.schedule_date ,
- count(cl_customer.id) as adhoc_customer,
- count(cl_lead.id) as adhoc_lead,
- count(cl_customer.id) + count(cl_lead.id) as total_adhoc,
- SUM(CASE WHEN cl_customer.status_crm_pipeline = 'close_and_complete' THEN 1 ELSE 0 END) as complete_customer,
- SUM(CASE WHEN cl_customer.status_crm_pipeline != 'close_and_complete' THEN 1 ELSE 0 END) as incomplete_customer,
- SUM(CASE WHEN cl_lead.status_crm_pipeline = 'close_and_complete' THEN 1 ELSE 0 END) as complete_lead,
- SUM(CASE WHEN cl_lead.status_crm_pipeline != 'close_and_complete' THEN 1 ELSE 0 END) as incomplete_lead,
- SUM(CASE WHEN cl_customer.status_call = 'invalid' THEN 1 ELSE 0 END) as invalid_customer,
- SUM(CASE WHEN cl_customer.status_call = 'not_answer' THEN 1 ELSE 0 END) as not_answer_customer,
- SUM(CASE WHEN cl_customer.status_call = 'answer' THEN 1 ELSE 0 END) as answer_customer,
- SUM(CASE WHEN cl_customer.status_call = 'chat_only' THEN 1 ELSE 0 END) as chat_only_customer,
- SUM(CASE WHEN cl_lead.status_call = 'invalid' THEN 1 ELSE 0 END) as invalid_lead,
- SUM(CASE WHEN cl_lead.status_call = 'not_answer' THEN 1 ELSE 0 END) as not_answer_lead,
- SUM(CASE WHEN cl_lead.status_call = 'answer' THEN 1 ELSE 0 END) as answer_lead,
- SUM(CASE WHEN cl_lead.status_call = 'chat_only' THEN 1 ELSE 0 END) as chat_only_lead
- -- SUM(CASE WHEN cl.status_crm_pipeline != 'close_and_complete' THEN 1 ELSE 0 END) as incomplete,
- -- SUM(CASE WHEN cl.status_meet_visit = 'meet_customer' THEN 1 ELSE 0 END) as meet,
- -- SUM(CASE WHEN cl.status_meet_visit != 'meet_customer' THEN 1 ELSE 0 END) as not_meet
- from crm_adhoc_plan cap
- left join crm_lead cl_customer on cap.schedule_date = cl_customer.schedule_date and cap.user_id = cl_customer.user_id and cap.partner_id = cl_customer.partner_id
- left join crm_lead cl_lead on cap.schedule_date = cl_lead.schedule_date and cap.user_id = cl_lead.user_id and cap.crm_lead_pool_id = cl_lead.crm_lead_pool_id
- left join res_users ru on cap.user_id = ru.id
- left join res_partner rp on cap.partner_id = rp.id
- left join crm_lead_pool clp ON cap.crm_lead_pool_id = clp.id
- left join res_branch rb on ru.branch_id = rb.id
- left join master_company_mnf rc on rb.company_id = rc.id
- where cap.schedule_date between '2023-08-01' and '2023-08-16' and cap.type_pipeline in ('call_plan_customer','call_plan_leads')
- group by cap.schedule_date, cap.user_id ,rc.name, rb.name,ru.login
Add Comment
Please, Sign In to add comment