How to get multiple counts with one SQL query?

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

    You can use a CASE statement with an aggregate function. This is basically the same thing as a PIVOT function in some RDBMS:

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

提交回复
热议问题