我正在尝试使用CASE从带有子查询的dblink返回值。但是,结果并未显示出我的期望。而不是通过每个WHEN,它从第一个WHEN返回值(即使它不符合条件)。我确定这与我在子查询中使用CASE的方式有关。
我自己运行了子查询,它返回了预期的结果。
select su.shp_filter as "User_Type", su.shp_access_id as "RAD_ID", su.shp_name as "User_Name", rg.rad_stu_level as "Student_Level",
case
when su.shp_filter = 'STU' then
case
when rgd.rad_goal_value like 'HP%' then 'HP'
when rgd.rad_goal_value like 'AS%' then 'AS'
when rgd.rad_goal_value like 'BU%' then 'BU'
when rgd.rad_goal_value like 'ED%' then 'ED'
when rgd.rad_goal_value like 'TE%' then 'TE'
when rgd.rad_goal_value like 'UN%' then 'UN'
when rgd.rad_goal_value like 'KE%' then 'KE'
when rgd.rad_goal_value like 'PH%' then 'PH'
when rgd.rad_goal_value like 'OP%' then 'OP'
when rgd.rad_goal_value like 'CP%' then 'CP'
end
when su.shp_filter in ('ADV','REG','DEAN','DEPT','ATHL') then
case
when exists (select pebempl.pebempl_orgn_code_home, su.shp_access_id, spriden.spriden_id from pebempl@dblink pebempl
join spriden@gold.ferris.edu spriden
on pebempl.pebempl_pidm=spriden.spriden_pidm
join DWSCHEMA.SHP_USER_MST su
on substr(su.shp_access_id,1,8) = substr(spriden.SPRIDEN_ID,1,8)
where spriden.spriden_change_ind is null
and pebempl.pebempl_orgn_code_home like '11%') then 'Office 1'
when exists (select pebempl.pebempl_orgn_code_home, su.shp_access_id, spriden.spriden_id from pebempl@dblink pebempl
join spriden@gold.ferris.edu spriden
on pebempl.pebempl_pidm=spriden.spriden_pidm
join DWSCHEMA.SHP_USER_MST su
on substr(su.shp_access_id,1,8) = substr(spriden.SPRIDEN_ID,1,8)
where spriden.spriden_change_ind is null
and pebempl.pebempl_orgn_code_home like '21%') then 'Office 2'
when exists (select pebempl.pebempl_orgn_code_home, su.shp_access_id, spriden.spriden_id from pebempl@dblink pebempl
join spriden@gold.ferris.edu spriden
on pebempl.pebempl_pidm=spriden.spriden_pidm
join DWSCHEMA.SHP_USER_MST su
on substr(su.shp_access_id,1,8) = substr(spriden.SPRIDEN_ID,1,8)
where spriden.spriden_change_ind is null
and pebempl.pebempl_orgn_code_home like '22%') then 'Office 3'
when exists (select pebempl.pebempl_orgn_code_home, su.shp_access_id, spriden.spriden_id from pebempl@dblink pebempl
join spriden@gold.ferris.edu spriden
on pebempl.pebempl_pidm=spriden.spriden_pidm
join DWSCHEMA.SHP_USER_MST su
on substr(su.shp_access_id,1,8) = substr(spriden.SPRIDEN_ID,1,8)
where spriden.spriden_change_ind is null
and pebempl.pebempl_orgn_code_home like '31%') then 'Academic Affairs'
end "College"
答案 0 :(得分:1)
哦-我想我知道您的问题。您的exists
子查询根本没有加入外部查询,因此它们总是返回数百行。如果将SHP_USER_MST的每个实例重命名为su1,su2,su3等,将会更加清楚:
when su.shp_filter in ('ADV','REG','DEAN','DEPT','ATHL') then
case
when exists (select pebempl.pebempl_orgn_code_home, su1.shp_access_id, spriden.spriden_id from pebempl@dblink pebempl
join spriden@gold.ferris.edu spriden
on pebempl.pebempl_pidm=spriden.spriden_pidm
join DWSCHEMA.SHP_USER_MST su1
on substr(su1.shp_access_id,1,8) = substr(spriden.SPRIDEN_ID,1,8)
where spriden.spriden_change_ind is null
and pebempl.pebempl_orgn_code_home like '11%') then 'Office 1'
when exists (select pebempl.pebempl_orgn_code_home, su2.shp_access_id, spriden.spriden_id from pebempl@dblink pebempl
join spriden@gold.ferris.edu spriden
on pebempl.pebempl_pidm=spriden.spriden_pidm
join DWSCHEMA.SHP_USER_MST su2
on substr(su2.shp_access_id,1,8) = substr(spriden.SPRIDEN_ID,1,8)
where spriden.spriden_change_ind is null
and pebempl.pebempl_orgn_code_home like '21%') then 'Office 2'
如果您在此处查看,则子查询中没有任何内容引用su
中的当前行-因此,您正在检查子查询中是否存在任何用户。而且他们总是这样做,因此它总是返回“ Office 1”。
我认为您想要更多类似
when su.shp_filter in ('ADV','REG','DEAN','DEPT','ATHL') then
case
when exists (select pebempl.pebempl_orgn_code_home, su.shp_access_id, spriden.spriden_id from pebempl@dblink pebempl
join spriden@gold.ferris.edu spriden
on pebempl.pebempl_pidm=spriden.spriden_pidm
where spriden.spriden_change_ind is null
and substr(su.shp_access_id,1,8) = substr(spriden.SPRIDEN_ID,1,8) -- Link subquery to outer query
and pebempl.pebempl_orgn_code_home like '11%') then 'Office 1'
when exists (select pebempl.pebempl_orgn_code_home, su.shp_access_id, spriden.spriden_id from pebempl@dblink pebempl
join spriden@gold.ferris.edu spriden
on pebempl.pebempl_pidm=spriden.spriden_pidm
where spriden.spriden_change_ind is null
and substr(su.shp_access_id,1,8) = substr(spriden.SPRIDEN_ID,1,8) -- Link subquery to outer query
and pebempl.pebempl_orgn_code_home like '21%') then 'Office 2'
...etc
我认为这应该对您有用,但是作为其他建议,我认为您也可以重写该部分,以使其更有效率和可读性。
case (select substr(min(pebempl.pebempl_orgn_code_home),1,2) from pebempl@dblink pebempl
join spriden@gold.ferris.edu spriden
on pebempl.pebempl_pidm=spriden.spriden_pidm
where spriden.spriden_change_ind is null
and substr(su.shp_access_id,1,8) = substr(spriden.SPRIDEN_ID,1,8))
when '11' then 'Office 1'
when '21' then 'Office 2'
when '22' then 'Office 3'
when '31' then 'Academic Affairs'
end