-- 這裡的create or replace function 為固定寫法: "public"."function_info_a1" 這個為函式名
create or replace function "public"."function_info_a1"(d1 varchar, d2 varchar, procuct varchar)
returns setof "pg_catalog"."record" as $body$
declare
rec record;
begin
--建立臨時表
create temp table flagtable(product_id int,rq timestamp,doctype varchar,docname varchar,qc float,rk float,ck float,jc float) on commit drop;
--on commit drop表示提交後會刪除
--插入資料
insert into flagtable(product_id,rq,doctype,docname,qc,rk,ck,jc)
--查詢資料
select t1.id,d1::timestamp,'期初','',0,0,0,0 from product_product t1 left join product_template t0 on t1.product_tmpl_id=t0.id
where t0.active='t' and t1.active='t' and t0.categ_id=6;
--所有成品的開始日期之前結存(期初)
--更新資料
update flagtable t3 set qc=(select
coalesce(sum(case when location_id =get_warehouse_id('成品倉') then - 1 * coalesce(product_qty,0) else coalesce(product_qty,0) end),0) qc
from stock_move ts
where state = 'done' and date< d1::timestamp
and (location_id =get_warehouse_id('成品倉') or location_dest_id =get_warehouse_id('成品倉')) and ts.product_id=t3.product_id );
--最初的期初和結存一樣的
update flagtable set jc=qc;
--插入時間段內的資料
insert into flagtable(product_id,rq,doctype,docname,qc,rk,ck,jc)
select product_id,ts1.date,case when ts3.name='internal transfers' then '調撥單' when ts3.name is null then '盤點單' else ts3.name end,
case when ts2.name is null then ts1.name else ts2.name end,0,coalesce (case when location_dest_id =get_warehouse_id('成品倉') then product_qty else 0 end,0),
coalesce (case when location_id =get_warehouse_id('成品倉') then product_qty else 0 end,0),0
from stock_move ts1 left join stock_picking ts2 on ts1.picking_id=ts2.id left join stock_picking_type ts3 on ts2.picking_type_id=ts3.id
where ts1.state = 'done' and ts1.date >=d1::timestamp and ts1.date <=d2::timestamp and (ts1.location_id =get_warehouse_id('成品倉') or ts1.location_dest_id =get_warehouse_id('成品倉'))
order by ts1.product_id;
--計算每條記錄的期初
update flagtable t4 set qc=coalesce(case when qc=0 then (select sum(jc+rk-ck) from flagtable t5 where t5.product_id=t4.product_id and t5.rq
--計算每個結存
update flagtable set jc=(qc+rk-ck)
where jc=0;
for rec in select rq,name_template,th,spec,doctype,docname,qc ,rk,ck,jc from (select product_id,a0.rq rq,a1.name_template,(select khwl_code from product_custo_info where a2.id=product_tmpl_id order by id limit 1) th,a2.spec,a0.doctype,a0.docname ,a0.qc ,a0.rk,a0.ck,a0.jc
from flagtable a0 left join product_product a1 on a0.product_id=a1.id left join product_template a2 on a1.product_tmpl_id=a2.id
where (a0.qc<>0 or a0.rk<>0 or a0.ck<>0 or a0.jc<>0))t where (t.th like '%'||procuct||'%' or name_template like '%'||procuct||'%'or spec like '%'||procuct||'%' )
order by product_id,rq
loop
return next rec;
end loop;
return;
end;
$body$
language 'plpgsql' volatile cost 100
rows 1000
;
postgrepsql簡單部署
2 解壓 tar xvf postgresql 10.0.tar.bz2 3 安裝 cd postgresql 10.0 configure prefix crmp sgwadm work zhangtaoa pg pghome make make install 4 建立data目錄 mkdir ...
postgrepsql原始碼安裝
通過rz r received 上傳檔案到伺服器,會彈出乙個視窗選擇本地檔案進行上傳。直接使用rz可能會報zmodem transfer canceled by remote side錯誤,使用rz e 就可以 解決這個問題。安裝rz yum y install lrzsz 上傳檔案到 usr pg...
PostGrepSql 執行緒池多執行緒資料插入
資料庫表複製,資料量大時,用單純的客戶端工具只能單執行緒匯入,時間耗時長。本文主要採用jdbc和執行緒池解決這個問題。jdbc 我用的 postgrepsql 資料庫,根據自己的資料庫 新增pom.xml postgresqlgroupid postgresqlartifactid 9.1 901 ...