oracle sum case when group by,同时使用,实现分组统计

    xiaoxiao2021-03-25  82

    如,我希望统计users表中每个创建者创建的男性、女性、无性别的用户总数(数据表中有一个字段creator_id,表示创建者的ID),语句如下: 

    select u.creator_id 创建者ID, 

    sum( case u.sex when 1 then 1 else 0 end) 男性, 

    sum( case u.sex when 2 then 1 else 0 end) 女性, 

    sum( case when u.sex<>1 and u.sex<>2 then 1 else 0 end) 性别为空 

    from users u group by u.creator_id;

    转载请注明原文地址: https://ju.6miu.com/read-17791.html

    最新回复(0)