select distinct col.collateral_number, CASE WHEN col.status = 1 THEN 'To be Taken' WHEN col.status = 15 THEN 'To be Deferred' WHEN col.status = 53 THEN 'To be Waived' WHEN col.status IN (5,57) THEN 'Held' ELSE F_GET_SYS_CODES_DESC(301700000,col.status) END AS held_taken, COL_REAL.APPRAISER_NAME valuation_name, -- VEHICLE CORE.APPRAISER_NAME valuat, --- OTHER THAN VEHICLE --CASE -- WHEN col.collateral_type IN (31,7) -- THEN COALESCE( F_GET_HBC_CODES_DESC(7078,COL_REAL.APPRAISER_NAME) ,F_GET_HBC_CODES_DESC(7078,CORE.APPRAISER_NAME) ) || ' | ' ||COALESCE(cast(to_char(COL_REAL.valuation_date_veh,'dd-mm-yyyy')as varchar),cast(to_char(CORE.VALUATION_DATE,'dd-mm-yyyy')as varchar)) -- ELSE 'NA' -- END AS val_date, CASE WHEN col.collateral_type = 7 THEN F_GET_HBC_CODES_DESC(7078,CORE.APPRAISER_NAME)|| ' | ' ||cast(to_char(CORE.VALUATION_DATE,'dd-mm-yyyy')as varchar) WHEN col.collateral_type = 31 THEN F_GET_HBC_CODES_DESC(7078,COL_REAL.APPRAISER_NAME)|| ' | ' ||cast(to_char(COL_REAL.valuation_date_veh,'dd-mm-yyyy')as varchar) ELSE 'NA' END AS val_date, --DECODE(col.collateral_type,31,(cast(to_char(COL_REAL.VALUATION_DATE,'dd-mm-yyyy')as varchar) || COL_REAL.PROPERTY_NAME),'NA') VALUATION_DATE, col.status code_value, col.collateral_type codeoftype, s.coll_type_desc as subtype , cast(to_char(col.expiry_date,'dd/mm/yyyy')as varchar) as valuation, trunc(col.collateral_amt,2) as market, -- remark1 trunc(col.coverage_amt_base,2) as extended , col.remarks, col.city , --remark2 col.coverage_amt_curr as curr from mst_clos_coll_sub_type s, BUS_CLOS_COLLATERAL col left join bus_clos_coll_vehicle COL_REAL on( COL_REAL.CIF_NUMBER = COL.CIF_NUMBER AND COL_REAL.APP_NUMBER = COL.APP_NUMBER AND COL_REAL.COLLATERAL_NUMBER = COL.COLLATERAL_NUMBER AND COALESCE( COL_REAL.LINKAGE_STATUS, 2 )!= 1) left join BUS_CLOS_COLL_REAL_ESTATE CORE on( CORE.CIF_NUMBER = COL.CIF_NUMBER AND CORE.APP_NUMBER = COL.APP_NUMBER AND CORE.COLLATERAL_NUMBER = COL.COLLATERAL_NUMBER AND COALESCE( CORE.LINKAGE_STATUS, 2 )!= 1 ) where col.cif_number = cast(100006746 as integer) and col.app_number = 0 and col.status in (1,5,15,53,57) and col.coll_sub_type = s.coll_typ_id and s.lng_code=1 ERROR coming after execution of this query for postgress ERROR: set-returning functions are not allowed in CASE Hint: You might be able to move the set-returning function into a LATERAL FROM item. Position: 649 fix this