Hi all i have 2 tables dcrhd ( which holds current data) and dcrhd_arc(which holds historical data) and I have created a function to get some data from theses tables.But this function satisfies only half of my requirement(it checking data from dcrhd table only) i will share my function here..
CREATE OR REPLACE FUNCTION dcr_report( --fin_year_flag,
finid integer, prdid integer, comp_cd CHARACTER varying, divid integer, fsid integer) RETURNS refcursor LANGUAGE 'plpgsql' AS $BODY$
DECLARE
ref refcursor;
BEGIN
open ref for SELECT hd.report_no,
hd.dcr_date,
coalesce(pr2.para_descr,' ') work_type,
coalesce(pr1.para_descr,' ') hq_type,
coalesce(rm.route_name,' ') route_name,
coalesce(hd.doctor_visits,0) doctor_visits,
coalesce(hd.stockist_visits,0) stockist_visits,
coalesce(hd.retailer_visits,0) retailer_visits,
hd.dcr_id,
fm.fs_name,
hd.fstaff_id,
CASE hd.status
WHEN 'A' THEN 'APPROVED'
WHEN 'D' THEN 'DISCARDED'
WHEN 'F' THEN 'FORWARDED'
WHEN 'E' THEN 'DRAFT'
END
status,
zsm.fs_name report1,
rsm.fs_name report2,
fm.geog_lvl1_hq,
fm.level_code,
coalesce(pm.para_descr,'SELF') joint_work,
fm.fs_code,
fm.emp_code,
coalesce(hd.doc_other,0) doc_other
FROM dcrhd hd
LEFT OUTER JOIN parameters pm ON hd.jfw = pm.para_code AND pm.para_type = 'JFW'
LEFT OUTER JOIN route_master rm ON rm.fstaff_id = hd.fstaff_id AND rm.route_id = hd.route_id AND rm.company_cd
= comp_cd
LEFT OUTER JOIN parameters pr1 ON pr1.para_code = hd.hq_exhq AND pr1.para_type = 'HQ_',
parameters pr2,
field_master fm,
field_master zsm,
field_master rsm
WHERE hd.period_id = prdid AND hd.fin_year_id = finid AND hd.fstaff_id = fm.fs_id AND fm.mgr_level4 =
zsm.fs_id AND fm.mgr_level3 = rsm.fs_id AND fm.fs_id =
CASE
WHEN fsid = 0 THEN fm.fs_id
ELSE fsid
END
AND fm.div_id =
CASE
WHEN divid = 0 THEN fm.div_id
ELSE divid
END
AND fm.fs_id = hd.fstaff_id AND fm.level_code = '005' AND pr2.para_code = hd.work_type AND pr2.
para_type = 'WTP' AND hd.company = comp_cd AND fm.company_cd = comp_cd
ORDER BY fm.fs_name,
dcr_date;
RETURN REF;
END;
$BODY$;
My requirement is I just want to add a new parameter called 'fin_year_flag'
and select the master table accordingly (like , if fin_year_flag='current'
then go to dcrhd
else goto dcrhd_arc
can I achive this???
Would you guys please share your ideas on this??? and is there any other way to full fill my requirement??I am new to PostgreSQL googled many times on internet but couldn't find anything helpful..