ลองเอา code นี้ไปวางไว้ในระบบ script แล้ว เรียกใช้งานครับ
var fhospitalcode:string;
begin
fhospitalcode := getsqldata('select hospitalcode from opdconfig');
hosxp_getdataset('delete from vn_stat where vn not in (select vn from ovst)');
hosxp_getdataset('insert ignore into vn_stat (vn,hn,vstdate,pttype,spclty) select vn,hn,vstdate,pttype,spclty from ovst ');
hosxp_getdataset('update ignore vn_stat set hn = (select hn from ovst where ovst.vn = vn_stat.vn) ');
hosxp_getdataset('update ignore vn_stat set vstdate = (select vstdate from ovst where ovst.vn = vn_stat.vn) ');
hosxp_getdataset('update ignore vn_stat set spclty = (select spclty from ovst where ovst.vn = vn_stat.vn) ');
hosxp_getdataset('update ignore vn_stat set pttype = (select pttype from ovst where ovst.vn = vn_stat.vn) ');
hosxp_getdataset('update ignore vn_stat set hospmain = (select hospmain from ovst where ovst.vn = vn_stat.vn) ');
hosxp_getdataset('update ignore vn_stat set hospsub = (select hospsub from ovst where ovst.vn = vn_stat.vn) ');
hosxp_getdataset('update ignore vn_stat set pttype_in_region = "Y" where (hospmain="'
+ fhospitalcode + '" or hospsub="' + fhospitalcode +
'") and pttype in (select pttype from pttype where region_type = 1 or region_type is null) ');
hosxp_getdataset('update ignore vn_stat set pttype_in_region = "N" where (hospmain<>"'
+ fhospitalcode + '" and hospsub<>"' + fhospitalcode +
'") and pttype in (select pttype from pttype where region_type = 1 or region_type is null) ');
hosxp_getdataset('update ignore vn_stat set pdx = (select icd10 from ovstdiag where ovstdiag.vn = vn_stat.vn and ovstdiag.diagtype="1" ) ');
hosxp_getdataset('update ignore vn_stat set main_pdx = (select substring(icd10,1,3) from ovstdiag where ovstdiag.vn = vn_stat.vn and ovstdiag.diagtype="1" ) ');
hosxp_getdataset('update ignore vn_stat set income = (select sum(sum_price) as income from opitemrece where opitemrece.vn = vn_stat.vn ) ');
hosxp_getdataset('update ignore vn_stat set sex = (select sex from patient where patient.hn = vn_stat.hn ) ');
hosxp_getdataset('update ignore vn_stat set cid = (select replace(cardno,"-","") from ptcardno where ptcardno.hn = vn_stat.hn and ptcardno.cardtype="01" ) ');
hosxp_getdataset('update ignore vn_stat set pcode = (select pcode from pttype where pttype.pttype = vn_stat.pttype) ');
hosxp_getdataset('update ignore vn_stat set aid = (select concat(chwpart,amppart,tmbpart) from patient where patient.hn = vn_stat.hn) ');
hosxp_getdataset('update ignore vn_stat set inc01 = (select sum(sum_price) as income from opitemrece,income where opitemrece.vn = vn_stat.vn and opitemrece.income = income.income and income.income_group="01" ) ');
hosxp_getdataset('update ignore vn_stat set inc02 = (select sum(sum_price) as income from opitemrece,income where opitemrece.vn = vn_stat.vn and opitemrece.income = income.income and income.income_group="02" ) ');
hosxp_getdataset('update ignore vn_stat set inc03 = (select sum(sum_price) as income from opitemrece,income where opitemrece.vn = vn_stat.vn and opitemrece.income = income.income and income.income_group="03" ) ');
hosxp_getdataset('update ignore vn_stat set inc04 = (select sum(sum_price) as income from opitemrece,income where opitemrece.vn = vn_stat.vn and opitemrece.income = income.income and income.income_group="04" ) ');
hosxp_getdataset('update ignore vn_stat set inc05 = (select sum(sum_price) as income from opitemrece,income where opitemrece.vn = vn_stat.vn and opitemrece.income = income.income and income.income_group="05" ) ');
hosxp_getdataset('update ignore vn_stat set inc06 = (select sum(sum_price) as income from opitemrece,income where opitemrece.vn = vn_stat.vn and opitemrece.income = income.income and income.income_group="06" ) ');
hosxp_getdataset('update ignore vn_stat set inc07 = (select sum(sum_price) as income from opitemrece,income where opitemrece.vn = vn_stat.vn and opitemrece.income = income.income and income.income_group="07" ) ');
hosxp_getdataset('update ignore vn_stat set inc08 = (select sum(sum_price) as income from opitemrece,income where opitemrece.vn = vn_stat.vn and opitemrece.income = income.income and income.income_group="08" ) ');
hosxp_getdataset('update ignore vn_stat set inc09 = (select sum(sum_price) as income from opitemrece,income where opitemrece.vn = vn_stat.vn and opitemrece.income = income.income and income.income_group="09" ) ');
hosxp_getdataset('update ignore vn_stat set inc10 = (select sum(sum_price) as income from opitemrece,income where opitemrece.vn = vn_stat.vn and opitemrece.income = income.income and income.income_group="10" ) ');
hosxp_getdataset('update ignore vn_stat set inc11 = (select sum(sum_price) as income from opitemrece,income where opitemrece.vn = vn_stat.vn and opitemrece.income = income.income and income.income_group="11" ) ');
hosxp_getdataset('update ignore vn_stat set inc12 = (select sum(sum_price) as income from opitemrece,income where opitemrece.vn = vn_stat.vn and opitemrece.income = income.income and income.income_group="12" ) ');
hosxp_getdataset('update ignore vn_stat set inc13 = (select sum(sum_price) as income from opitemrece,income where opitemrece.vn = vn_stat.vn and opitemrece.income = income.income and income.income_group="13" ) ');
hosxp_getdataset('update ignore vn_stat set inc14 = (select sum(sum_price) as income from opitemrece,income where opitemrece.vn = vn_stat.vn and opitemrece.income = income.income and income.income_group="14" ) ');
hosxp_getdataset('update ignore vn_stat set inc15 = (select sum(sum_price) as income from opitemrece,income where opitemrece.vn = vn_stat.vn and opitemrece.income = income.income and income.income_group="15" ) ');
hosxp_getdataset('update ignore vn_stat set inc16 = (select sum(sum_price) as income from opitemrece,income where opitemrece.vn = vn_stat.vn and opitemrece.income = income.income and income.income_group="16" ) ');
hosxp_getdataset('update ignore vn_stat set inc17 = (select sum(sum_price) as income from opitemrece,income where opitemrece.vn = vn_stat.vn and opitemrece.income = income.income and income.income_group="17" ) ');
hosxp_getdataset('update ignore vn_stat set count_in_year = (select count(vn) from ovst where ovst.hn = vn_stat.hn and substring(ovst.vn,1,2) = substring(vn_stat.vn,1,2) and ovst.vn < vn_stat.vn) ');
hosxp_getdataset('update ignore vn_stat set count_in_month = (select count(vn) from ovst where ovst.hn = vn_stat.hn and substring(ovst.vn,1,4) = substring(vn_stat.vn,1,4) and ovst.vn < vn_stat.vn) ');
hosxp_getdataset('update ignore vn_stat set count_in_day = (select count(vn) from ovst where ovst.hn = vn_stat.hn and substring(ovst.vn,1,6) = substring(vn_stat.vn,1,6) and ovst.vn < vn_stat.vn) ');
end;