decode(a.revsal_flag,'R','Y',' ','N',a.revsal_flag) 转换成 case when
时间: 2024-01-12 11:02:56 浏览: 26
将 decode(a.revsal_flag,'R','Y',' ','N',a.revsal_flag) 转换成 case when 可以这样写:
```
case a.revsal_flag
when 'R' then 'Y'
when ' ' then 'N'
else a.revsal_flag
end
```
其中,第一个 when 子句表示如果 a.revsal_flag 的值为 'R',则返回 'Y';第二个 when 子句表示如果 a.revsal_flag 的值为空格,返回 'N';否则返回 a.revsal_flag 的原值。
相关问题
select sum(case when c.id_Dep_Pi_Af = '60877de521997e2901a929a1' and c.sd_Apply_Ap_Cd in ('1', '5') then 1 else 0 end) as depPiInNum, SUM(DECODE(c.ID_DEP_PI_BEF, '60877de521997e2901a929a1', 1, 0)) as depPiOutNum from Hi_Vis_Med a left join Hi_Vis_Med_Ip b on a.id_Vismed = b.id_Vismed left join Hi_Vis_Med_Dep_Pi_Changeap c on b.id_Vismed = c.id_Vismed where a.sd_Vistp_Cd = '114' and a.delete_Flag = 0 and c.sd_Apply_Ap_Cd in ('1', '2', '5', '6'); sql优化
可以尝试以下优化方式:
1. 创建索引:可以在Hi_Vis_Med、Hi_Vis_Med_Ip、Hi_Vis_Med_Dep_Pi_Changeap表中分别创建以下列的索引:id_Vismed、sd_Vistp_Cd、delete_Flag、id_Dep_Pi_Af、sd_Apply_Ap_Cd、ID_DEP_PI_BEF等。
2. 使用EXISTS代替LEFT JOIN:可以将LEFT JOIN改为EXISTS,如下所示:
select sum(case when c.id_Dep_Pi_Af = '60877de521997e2901a929a1' and c.sd_Apply_Ap_Cd in ('1', '5') then 1 else 0 end) as depPiInNum,
SUM(DECODE(c.ID_DEP_PI_BEF, '60877de521997e2901a929a1', 1, 0)) as depPiOutNum
from Hi_Vis_Med a
where a.sd_Vistp_Cd = '114' and a.delete_Flag = 0
and exists (
select 1 from Hi_Vis_Med_Ip b, Hi_Vis_Med_Dep_Pi_Changeap c
where a.id_Vismed = b.id_Vismed and b.id_Vismed = c.id_Vismed and c.sd_Apply_Ap_Cd in ('1', '2', '5', '6')
);
3. 将DECODE函数转成CASE语句:可以将DECODE函数转成CASE语句,如下所示:
select sum(case when c.id_Dep_Pi_Af = '60877de521997e2901a929a1' and c.sd_Apply_Ap_Cd in ('1', '5') then 1 else 0 end) as depPiInNum,
SUM(CASE WHEN c.ID_DEP_PI_BEF = '60877de521997e2901a929a1' THEN 1 ELSE 0 END) as depPiOutNum
from Hi_Vis_Med a
where a.sd_Vistp_Cd = '114' and a.delete_Flag = 0
and exists (
select 1 from Hi_Vis_Med_Ip b, Hi_Vis_Med_Dep_Pi_Changeap c
where a.id_Vismed = b.id_Vismed and b.id_Vismed = c.id_Vismed and c.sd_Apply_Ap_Cd in ('1', '2', '5', '6')
);
DECODE(A.ORDER_STATE, '10', '1', A.ORDER_STATE) STATE
根据您提供的 DECODE 函数示例,它的作用是将 A.ORDER_STATE 的值进行转换。如果 A.ORDER_STATE 的值为 '10',则返回 '1',否则返回 A.ORDER_STATE 的原始值。
在这个例子中,DECODE 函数的语法如下:
```
DECODE(A.ORDER_STATE, '10', '1', A.ORDER_STATE) STATE
```
其中:
- A.ORDER_STATE:要比较的值,即订单状态。
- '10':条件表达式的值,当 A.ORDER_STATE 等于 '10' 时满足条件。
- '1':条件表达式的结果,当 A.ORDER_STATE 等于 '10' 时返回的结果。
- A.ORDER_STATE:默认结果,当 A.ORDER_STATE 不等于 '10' 时返回的结果。
最终结果将命名为 STATE,并包含转换后的值。
请注意,DECODE 函数在某些数据库管理系统中可能不被支持,您可能需要根据您所使用的数据库类型进行相应的调整或使用其他函数来实现相同的功能。