Case 条件运算符
Case运算有两种写法,平常用的都比较多,这里只简单复习下,Case的语法帮助里就好,这里我尝试用颜色区分的方式让大家一眼就能了解其结构:
写法一:)
select
case when PriceType=1 then
case when serviceItemCode = '018' then '1-018'
when serviceItemCode = '019' then '1-019'
end
when PriceType=0 then
case when serviceItemCode = '001' then '0-001'
end
else '其它'
end as 'case计算',serviceItemCode,ServiceItemName,PriceType
from CSM_Bas_ServiceItem
order by 'case计算' desc
写法二:)
select
case PriceType
when 1 then
case serviceItemCode
when '018' then '1-018'
when '019' then '1-019'
end
when 0 then
case serviceItemCode
when '001' then '0-001'
end
else '其它'
end as 'case计算',serviceItemCode,ServiceItemName,PriceType
from CSM_Bas_ServiceItem
order by 'case计算' desc
case计算 serviceItemCode ServiceItemName PriceType ------ -------------------- ---------------------------------------- --------- 1-019 019 香港菲力士健身 1 1-019 019 壹宝贝早教 1 1-018 018 壹宝贝早教 1 1-018 018 香港菲力士健身 1 0-001 001 单次理发(其他) 0