case when 語句
一、概述
1.這個語句使用起來像decode函式加強版,9i之後的多重條件分支語句,decode配合例如sign類似的函式也能達到case when的效果,但是case when效率更高,需要注意的是,每個值返回的型別需要一致。
2.case語句可以實現if語句的功能。
3.case語句分為單一選擇符進行等值比較和多種條件進行非等值比較。
4.為了避免case_not_found異常,在編寫case語句時應該帶else子句
二、單一選擇符進行等值比較
說明:當使用case語句執行多重條件分支時,如果條件選擇符完全相同,並且條件表示式為相等條件選擇。
declare
v_deptno emp.deptno%type;
begin
v_deptno:=&no;
case v_deptno
when 10 when update emp set comm=100 where deptno=v_deptno;
when 20 when update emp set comm=80 where deptno=v_deptno;
when 30 when update emp set comm=60 where deptno=v_deptno;
else dbms_output.put_line('不存在該部門');
end case;
end;
三、多種條件進行非等值比較
說明:對於包含有多種條件進行不等比較,那麼必須在when子句中指定比較條件。
select b.name area_name,b.id area_id,3 type,a.id tran_id,a.name name,
case
when a.area_id = 0 and si_id = 0 then '省通用'
when a.si_id = 0 then '市級用'
else c.company
end company,
c.sp_id
from preferential_package a
left join qx_admin_si c on a.si_id = c.id
left join area b on a.area_id = b.id
order by area_name, a.name;
select town.area_id as area_id,town.id as town_id,school.xtown_id,school.id as school_id,
count (distinct student.stu_sequence),
count(case
when nvl (package.pp_id, 0) = 403 and family.charge = 1 then family.stu_sequence
else null
end) pak_403_chg,
count(case
when nvl (package.pp_id, 0) = 403 and family.charge = 0 then family.stu_sequence
else null
end) pak_403_try,
count(case
when nvl (package.pp_id, 0) = 402 and family.charge = 1 then family.stu_sequence
else null
end) pak_402_chg,
count(case
when nvl (package.pp_id, 0) = 402 and family.charge = 0 then family.stu_sequence
else null
end) pak_402_try,
count(case
when nvl (package.pp_id, 0) = 360 and family.charge = 1 then family.stu_sequence
else null
end) pak_360_chg,
count(case
when nvl (package.pp_id, 0) = 360 and family.charge = 0 then family.stu_sequence
else null
end) pak_360_try,
count(case
when nvl (package.pp_id, 0) = 404 then family.stu_sequence
else null
end) pak_404,
count(case
when nvl (package.pp_id, 0) = 440 then family.stu_sequence
else null
end) pak_440,
count(case
when nvl (package.pp_id, 0) = 420 then family.stu_sequence
else null
end) pak_420
from fs_xj_family family left join fs_preferential_packager package on package.f_id = family.id and package.del = 1,
fs_xj_student student,fs_xj_stu_class stuclazz,xj_class clazz,xj_school school,town town
where family.stu_sequence = stuclazz.stu_sequence
and stuclazz.stu_sequence = student.stu_sequence and stuclazz.class_id = clazz.id
and clazz.school_id = school.id and school.town_id = town.id
and nvl (town.is_test, 0) = 0 and clazz.in_school = 1 and clazz.class_type = 1 and family.phonetype = 0
group by town.area_id,town.id,school.xtown_id,school.id;
R語言 case when 函式
要點有兩個 不匹配的時候會返回 na,而不是保持不變 根據順序進行條件判斷,順序很重要 下面這段 x 1 50 case when x 35 0 fizz buzz x 5 0 fizz x 7 0 buzz true as.character x 如果不包含true as.character x ...
R語言 case when 函式
要點有兩個 不匹配的時候會返回 na,而不是保持不變 根據順序進行條件判斷,順序很重要 下面這段 x 1 50 case when x 35 0 fizz buzz x 5 0 fizz x 7 0 buzz true as.character x 如果不包含true as.character x ...
Hive常用函式 case when練習 一
原始資料 姓名,部門,性別 悟空 a 男 娜娜 a 男 宋宋 b 男 鳳姐 a 女 熱巴 b 女 慧慧 b 女建表,匯入資料 create table tb case name string dname string gender string row format delimited fields...