按条件count ,一条SQL里面

1
2
3
4
5
6
7
8
9
10
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 @   了悟  阅读(83)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
点击右上角即可分享
微信分享提示