How to get multiple counts with one SQL query?

后端 未结 9 2220
清酒与你
清酒与你 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:24

    Well, if you must have it all in one query, you could do a union:

    SELECT distributor_id, COUNT() FROM ... UNION
    SELECT COUNT() AS EXEC_COUNT FROM ... WHERE level = 'exec' UNION
    SELECT COUNT(*) AS PERSONAL_COUNT FROM ... WHERE level = 'personal';
    

    Or, if you can do after processing:

    SELECT distributor_id, COUNT(*) FROM ... GROUP BY level;
    

    You will get the count for each level and need to sum them all up to get the total.

提交回复
热议问题