PostgreSQL error: set-returning functions are not allowed in CASE

srinu picture srinu · Nov 30, 2017 · Viewed 10.8k times · Source

I tried to run this query in PostgreSQL 10:

select e.errordescription,
       CASE 
        WHEN e.reworkempid is not null THEN get_empname(e.reworkempid) 
        else null 
      end  
from error_log_gs  e 
where e.qcworkpackageid=3012175 and e.logno=1 

Got the error:

set-returning functions are not allowed in CASE

Answer

Gordon Linoff picture Gordon Linoff · Nov 30, 2017

Use a lateral join instead:

select e.errordescription, ge.name
from error_log_gs e left join lateral
     get_empname(e.reworkempid) ge(name)
     on e.reworkempid is not null
where e.qcworkpackageid = 3012175 and e.logno = 1 ;