码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • postgresql-条件表达式


    postgresql-条件表达式

    • 简单Case表达式
    • 搜索Case表达式
    • 缩写函数
    • 总结

    简单Case表达式

    select 
    e.first_name ,
    e.last_name ,
    e.department_id ,
    case e.department_id 
     when 90 then '管理'
     when 60 then '开发'
     else '其他'
     end as "部门"
    from cps.public.employees e ;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    -- 统计部门员工数量
    select 
    -- 部门id为10,返回1
    count(case e.department_id when 10 then 1 end) dept10_count,
    -- 部门id为20,返回1
    count(case e.department_id when 20 then 1 end) dept20_count,
    -- 部门id为30,返回1
    count(case e.department_id when 30 then 1 end) dept30_count
    from cps.public.employees e ;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    在这里插入图片描述

    -- 统计部门员工数量
    select 
    -- 统计部门id为10的员工数量
    count(*) filter(where e.department_id = 10) dept10_count,
    -- 统计部门id为20的员工数量
    count(*) filter(where e.department_id = 20) dept20_count,
    -- 统计部门id为30的员工数量
    count(*) filter(where e.department_id = 30) dept30_count
    from cps.public.employees e ;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    在这里插入图片描述

    搜索Case表达式

    select
    	e.first_name ,
    	e.last_name ,
    	case
    		when e.salary <5000 then '低收入'
    		when e.salary between 5000 and 10000 then '中等收入'
    		else '高收入'
    	end as salarySummary
    from
    	cps.public.employees e;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    在这里插入图片描述

    缩写函数

    /*
     * NULLIF 函数包含 2 个参数,如果第一个参数等于第二个参数,返回 NULL;否则,返回第一个参数的值
    */
    -- 除数为0返回1
    select 1/nullif(1,0) as result; 
    
    • 1
    • 2
    • 3
    • 4
    • 5

    在这里插入图片描述

    /*
     * COALESCE 函数接受多个参数,返回第一个null的参数值,挨个判断,如果所有的参数都为null,就返回null
     * */
    select coalesce(null,1,2) as finalResult;
    
    • 1
    • 2
    • 3
    • 4

    在这里插入图片描述

    select 
    e.first_name ,
    -- 奖金为空,就返回0
    coalesce(e.commission_pct,0) as jintie
    from cps.public.employees e ;
    
    • 1
    • 2
    • 3
    • 4
    • 5

    在这里插入图片描述

    总结

    在这里插入图片描述

  • 相关阅读:
    函数组件也可以通过connect获取react-redux的数据(reducer)
    echarts自定义图例(legend)样式
    [LeetCode算法]有了二叉树层序遍历,妈妈再也不用担心我不会做二叉树层级题了
    pta java版
    LVS负载均衡(LVS简介、三种工作模式、十种调度算法)
    记账时,花费如何按类别分类
    如何确认栈中申请的变量地址
    【长难句分析精讲】状语从句
    Vue插槽
    【YOLOv5入门】目标检测
  • 原文地址:https://blog.csdn.net/Java_Fly1/article/details/132641014
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号