postgrepsql 创建函数
-- 这里的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<t4.rq) else qc end,0);
--计算每个结存
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 创建函数的更多相关文章
- Sql Server创建函数
在使用数据库的过程中,往往我们需要对有的数据先进行计算,然后再查询出来,所以我们就需要创建函数来完成这项任务,在数据库的Programmability(如图1)下面的Function中创建函数(如图2 ...
- Python 动态创建函数【转】
知乎上也有相似的问题 偶然碰到一个问题,初想是通过动态创建Python函数的方式来解决,于是调研了动态创建Python函数的方法. 定义lambda函数 在Python中定义lambda函数的写法很简 ...
- 从new Function创建函数联想到MVC模式
我们知道任何一个自定义函数都是Function构造器的实例,所以我们可以通过new Function的方式来创建函数,使用语法很简单, new Function(形参1, 形参2, ..., 形参N, ...
- 进程创建函数fork()、vfork() ,以及excel()函数
一.进程的创建步骤以及创建函数的介绍 1.使用fork()或者vfork()函数创建新的进程 2.条用exec函数族修改创建的进程.使用fork()创建出来的进程是当前进程的完全复制,然而我们创建进程 ...
- Mysql创建函数出错
目前在项目中,执行创建mysql的函数出错, mysql 创建函数出错信息如下: Error Code: 1227. Access denied; you need (at least one of) ...
- mysql 创建函数set global log_bin_trust_function_creators=TRUE;
<pre name="code" class="html">set global log_bin_trust_function_creators=T ...
- mysql 创建函数
<pre name="code" class="html">root 用户创建函数: delimiter $$ CREATE FUNCTION `l ...
- MySQL 创建函数(Function)
目标 怎么样MySQL创建数据库功能(Function) 语法 CREATE FUNCTION func_name ( [func_parameter] ) //括号是必须的,參数是可选的 RETUR ...
- 如果是在有master上开启了该参数,记得在slave端也要开启这个参数(salve需要stop后再重新start),否则在master上创建函数会导致replaction中断。
如果是在有master上开启了该参数,记得在slave端也要开启这个参数(salve需要stop后再重新start),否则在master上创建函数会导致replaction中断.
随机推荐
- CloudSim源代码学习——任务单元(Cloudlet)
/* * Title: CloudSim Toolkit * Description: CloudSim (Cloud Simulation) Toolkit for Modeling and Sim ...
- python txt文件数据转excel
txt content: perf.txt 2018-11-12 16:48:58 time: 16:48:58 load average: 0.62, 0.54, 0.56 mosquitto CP ...
- python中关于类隐藏属性的三种处理方法
关于隐藏属性 引子: 当类的属性或者类实例对象的属性隐藏的时候必须通过存取器方法来获取和设置这些隐藏的属性. 例如: def get_name(self,name): #存取器方法 self. ...
- recovery 强制执行恢复出厂设置(Rescue Party)
有时候我们在系统正常运行的时候,突然跑到recovery里面了,并且停在了如下界面: Can't load Android system. Your data may be corrupt. If ...
- pyhthon常用模块hashlib
python hashlib模块 一,hashlib模块主要用于加密,其中提供sha1,sha224,sha256,sha384,sha512,md5算法.常用的使用md5即可完成需求. 一,使用md ...
- forfiles命令详解
目录复制命令: xcopy //server/bak/*.* d:/serverbak /s /e /v /c / d /y /h at 05:30 shutdown ...
- 第一章 Bootstrasp起步
模板如下: <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf ...
- bootstrap table使用及遇到的问题
本人前端菜鸟一枚,最近使用bootstrap table实现表格,记录一下以便日后翻阅,废话不多说,先看效果图: 1.首先说下要实现该效果需要添加的css样式及所需的js文件,具体下载地址就不粘贴了( ...
- Django REST framework 之分页,视图,路由,渲染器
1.分页 2.视图 3.路由 4.渲染器 1.分页 方法一: from django.shortcuts import render from rest_framework.versioning im ...
- exec dbms_stats.gather_schema_stats 手动优化统计
Oracle10g或以上版本.exec dbms_stats.gather_schema_stats(ownname => 'DFMS', options => 'GATHER AUTO' ...