美文网首页pg
sql casewhen函数,列转行

sql casewhen函数,列转行

作者: 饱饱想要灵感 | 来源:发表于2022-07-20 11:43 被阅读0次
    1. 简单查询, 类似switch-case
    CASE sex
    WHEN '1' THEN '男'
    WHEN '0' THEN '女'
    ELSE '其他' END
    
    1. 复杂查询, 类似if-else, pg的case when null实现需采取此用法
    CASE WHEN sex = '1' THEN '男'
    WHEN sex <>  '0' THEN '非女'
    ELSE '其他' END
    
    1. 构造列转行, 分类统计, 即case-when外面再套一个统计函数
    select sum(case when score >= 90 then 1 else 0 end) as 优秀人数,
    sum(case when score < 90 then 1 else 0 end) as 不优秀人数,
    sum(case when score is null then 1 else 0 end) as 未统计人数,
    count(*) as 总人数
    from user;
    

    相关文章

      网友评论

        本文标题:sql casewhen函数,列转行

        本文链接:https://www.haomeiwen.com/subject/sfbdirtx.html