mysql学习教程参考:https://www.w3school.com.cn/sql/index.asp
where 与 having by实例运用参考:https://www.2cto.com/database/201712/706595.html
 
 
--可选认证中至少有一项通过的用户占比,直接计算
select 
 count(distinct case when c.auth_type=‘可选‘ and c.auth_status in (‘已认证‘, ‘已过期‘) then c.user_id end) as pass_cnt
,count(distinct c.user_id) as tot_cnt
,count(distinct case when c.auth_type=‘可选‘ and c.auth_status in (‘已认证‘, ‘已过期‘) then c.user_id end)/count(distinct c.user_id) per_cnt
from(
 select a.*,ath.auth_item,ath.auth_type,ath.auth_status,ath.auth_suc_time 
  from(
   select laf.user_id,laf.main_order_id,laf.loan_apply_submit_time
   from pl_performance_loan_apply_fact laf
   left join pl_customer_basic_info usb on laf.user_id=usb.user_id
   where laf.reborrow_status = ‘首借‘ and usb.merchant_no=‘DuitSayang‘ 
      and date(laf.loan_apply_submit_time)>=‘2019-08-03‘ and date(laf.loan_apply_submit_time)<=‘2019-08-05‘
) a 
left join pl_customer_auth_info ath on a.user_id = ath.user_id
) c
;
 
 
--找出未通过认证的用户,巧用having by 
select a.user_id,a.phone 
  from(
   select laf.user_id,laf.main_order_id,laf.loan_apply_submit_time,usb.phone
   from pl_performance_loan_apply_fact laf
   left join pl_customer_basic_info usb on laf.user_id=usb.user_id
   where laf.reborrow_status = ‘首借‘ and usb.merchant_no=‘DuitSayang‘ 
      and date(laf.loan_apply_submit_time)>=‘2019-08-03‘ and date(laf.loan_apply_submit_time)<=‘2019-08-05‘
) a 
left join pl_customer_auth_info ath on a.user_id = ath.user_id
group by a.user_id,a.phone 
having max( case when ath.auth_type=‘可选‘ and ath.auth_status in (‘已认证‘, ‘已过期‘) then 1 else 0 end) = 0
 
 巧用having by
原文:https://www.cnblogs.com/dlp-527/p/11304503.html