按条件count ,一条SQL里面

select eq_no,
    count(if(out_in_type = '01',1,null)) as inCount,  //01 进场人次
    count(if(out_in_type = '02',1,null)) as OutCount, //02 出场人次
    ent_name,
    ent_id,
    COUNT(DISTINCT id_card_no, if(out_in_type = '01',1,null)) as inCountPeople,  // 按身份证号去重后的进场人数
    COUNT(DISTINCT id_card_no, if(out_in_type = '02',1,null)) as OutCountPeople,
    out_in_type
from t_tm_eq_scan_record
group by eq_no

  

 

但是最好别这么干,效率很低。 统计行数最好单独做个表,在条件查询影响不大的情况下

posted @ 2022-03-15 15:34  了悟  阅读(88)  评论(0)    收藏  举报