How to get multiple counts with one SQL query?

后端 未结 9 2051
清酒与你
清酒与你 2020-11-22 14:03

I am wondering how to write this query.

I know this actual syntax is bogus, but it will help you understand what I am wanting. I need it in this format, because it i

9条回答
  •  清酒与你
    2020-11-22 14:05

    SELECT 
        distributor_id, 
        COUNT(*) AS TOTAL, 
        COUNT(IF(level='exec',1,null)),
        COUNT(IF(level='personal',1,null))
    FROM sometable;
    

    COUNT only counts non null values and the DECODE will return non null value 1 only if your condition is satisfied.

提交回复
热议问题