sql case when then 写法

本文通过两个实例详细解析了 SQL 中 Case When 的使用方法。第一个实例展示了如何根据不同条件选择显示不同的单位名称和数量,第二个实例则介绍了如何使用 Case When 对查询结果进行排序。这两个例子涵盖了 Case When 在数据筛选和排序中的基本应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

case when 查询
select 
    scrap_detl_id,scrap_ou_qty,scrap_su_qty,scrap_bu_qty,  
    (
        CASE
        WHEN scrap_ou_qty NOTNULL and to_number(scrap_ou_qty || '','9999999') > 0 
        THEN order_unit_name
        WHEN scrap_su_qty NOTNULL and to_number(scrap_su_qty || '','9999999') > 0 
        then unit_name
        ELSE bom_unit_name
    END) AS scrap_unit_name,
    (
        CASE
        WHEN scrap_ou_qty NOTNULL and to_number(scrap_ou_qty || '','9999999') > 0 
        THEN scrap_ou_qty
        WHEN scrap_su_qty NOTNULL and to_number(scrap_su_qty || '','9999999') > 0 
        then scrap_su_qty
        ELSE scrap_bu_qty
    END) AS scrap_qty

from tb_scrap_detl

 

 

order by case when then 写法

select user_name,user_id,role from tb_user  

where company_id = 'JH'

and parent_id = 'JH'

order by  case  

when role = 'SM'

 then 0

else 1

end
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值