• 2023.11.16 hivesql之条件函数,case when then


    目录

    一.Conditional Functions条件函数

    二.空值相关函数 

    三:使用注意事项

    3.1 then后面不能接子查询

    3.2 then后面只能是结果值

    3.3 then后面能不能接两列

    四.用于建表新增字段使用场景


    一.Conditional Functions条件函数

    1. -- 演示条件函数
    2. -- if(条件判断,true的时候执行此处,false的时候执行此处)
    3. select if(10 > 5, '真', '假'); --如果为true,执行前面的.如果为false,执行后面的
    4. select if(10 < 5, '真', '假');
    5. --条件转换函数格式1: CASE a WHEN b THEN c [WHEN d THEN e]* [ELSE f] END

    演示:

    1. select
    2. case 7 --拿着7这个结果,依次在下面各个对比
    3. when 1 then '周一上班'
    4. when 2 then '周二上班'
    5. when 3 then '周三上班'
    6. when 4 then '周四上班'
    7. when 5 then '周五上班'
    8. when 6 then '周六休息'
    9. when 7 then '周日休息'
    10. else '老弟啊,你是外星人吗?'
    11. end;
    12. -- 条件转换函数格式2:CASE WHEN a==b THEN a==c [WHEN a==d THEN a==e]* [ELSE f] END
    13. select
    14. case
    15. when 7==1 then '周一上班'
    16. when 7==2 then '周二上班'
    17. when 7==3 then '周三上班'
    18. when 7==4 then '周四上班'
    19. when 7==5 then '周五上班'
    20. when 7==6 then '周六休息'
    21. when 7==7 then '周日休息'
    22. else '老弟啊,你是外星人吗?'
    23. end;

    二.空值相关函数 

    1. -- 演示null相关函数
    2. -- isnull(数据) 为空: true 不为空:false
    3. select isnull(null); -- true
    4. -- isnotnull(数据) 不为空: true 为空:false
    5. select isnotnull('斌子'); -- true
    6. -- nvl(数据,前面的数据是null的时候执行此处): 如果数据不为空打印数据,为空打印第二个参数
    7. select nvl('binzi','666');
    8. select nvl(null,'666');
    9. -- coalesce(v1,v2...): 从左到右依次查找,返回第一个不是null的值,如果找到最后都是null,就返回null
    10. -- 常用于判断某些字段是否是null的
    11. select COALESCE(null,11,22,33);-- 11
    12. select COALESCE(null,null,22,33);--22
    13. select COALESCE(null,null,null,33);--33
    14. select COALESCE(null,null,null,0);--0
    15. select COALESCE(null,null,null,null);--null

    三:使用注意事项

    3.1 then后面不能接子查询

     then的后面不能接查询语句

    3.2 then后面只能是结果值

    then的后面只能接具体的值,不能接逻辑表达式

    3.3 then后面能不能接两列

    then的后面不能接两列的值,如果想要接两列的值,就必须写两遍case when then逻辑。

     

    四.用于建表新增字段使用场景

    现有一张订单表,其中有支付类型,支付方式,订单状态,这三个字段是使用0,1,2,3数字来定义状态的

     在加载完文件后,表的几个字段内容如下

    在原表中,用0,1,2,3这样的数字来确认状态,不方便查看,因此我们可以使用case条件判断,

    when 数字=0,1,2 ,then内容显示对应的中文状态,来方便我们查看

    1. create table dw1_orders as
    2. select
    3. orderid,
    4. orderno,
    5. shopid,
    6. userid,
    7. orderStatus, --这里如果不写原来的字段名,那么新增的中文列就替代原来的这列
    8. case
    9. when orderStatus = -3 then '用户拒收'
    10. when orderStatus = -2 then '未付款的订单'
    11. when orderStatus = -1 then '用户取消'
    12. when orderStatus = 0 then '待发货'
    13. when orderStatus = 1 then '配送中'
    14. when orderStatus = 2 then '用户确认收货'
    15. end as orderStatus_cn, -- 相当于新增了一列
    16. goodsmoney,
    17. delivermoney,
    18. totalmoney,
    19. realtotalmoney,
    20. payType,
    21. case
    22. when payType = 0 then '未知'
    23. when payType = 1 then '支付宝'
    24. when payType = 2 then '微信'
    25. when payType = 3 then '现金'
    26. when payType = 4 then '其他'
    27. end as paytype_cn
    28. ,
    29. isPay,
    30. case
    31. when isPay = 0 then '未知'
    32. when isPay = 1 then '支付宝'
    33. when isPay = 2 then '微信'
    34. when isPay = 3 then '现金'
    35. when isPay = 4 then '其他'
    36. end as isPay_cn,
    37. username,
    38. useraddress,
    39. userphone,
    40. createtime,
    41. paytime,
    42. totalpayfee
    43. from orders;

    创建的表效果如下,在原来的0,1,2旁边新增了一列对应的中文,方便了我们查看

    也可以用overwrite操作来覆盖原来的表,但我们是新增了三列,所以列数和原表对不上,这里就当做新表了.

  • 相关阅读:
    手写RPC框架
    【极客时间-系列教程】深入剖析Kubernetes-预习篇 · 小鲸鱼大事记(二):崭露头角
    免费mes/开源MES/快速解决工厂生产管理难题
    安全队列和曲线拟合
    MCollections——16
    Hbase 面试题(五)
    Go语言学习笔记-A Tour of Go 练习笔记-Maps
    GBASE 8s 并行机制之并行DML操作
    pytorch教程
    shiro的会话管理器SessionManager
  • 原文地址:https://blog.csdn.net/m0_49956154/article/details/134416357