How to get multiple counts with one SQL query?

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

    Based on Bluefeet's accepted response with an added nuance using OVER():

    SELECT distributor_id,
        COUNT(*) total,
        SUM(case when level = 'exec' then 1 else 0 end) OVER() ExecCount,
        SUM(case when level = 'personal' then 1 else 0 end) OVER () PersonalCount
    FROM yourtable
    GROUP BY distributor_id
    

    Using OVER() with nothing in the () will give you the total count for the whole dataset.

提交回复
热议问题