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中断.
随机推荐
- layer.open
1.type-基本层类型 类型:Number,默认:0 layer提供了5种层类型.可传入的值有:0(信息框,默认)1(页面层)2(iframe层)3(加载层)4(tips层). 若你采用layer. ...
- 微信小程序 thirdScriptError sdk uncaught third Error regeneratorRuntime is not defined ReferenceError: regeneratorRuntime is not defined
thirdScriptError sdk uncaught third Error regeneratorRuntime is not defined ReferenceError: regenera ...
- 卷积神经网络CNNs的理解与体会
https://blog.csdn.net/shijing_0214/article/details/53143393 孔子说过,温故而知新,时隔俩月再重看CNNs,当时不太了解的地方,又有了新的理解 ...
- Apex简介
Apex特点 Salesforce为开发者提供了Apex语言.它是一种语法上类似于Java的编程语言,有以下特点: 面向对象 完全在云端处理,包括保存.编译.执行 强类型 大小写不敏感(这一点和其他大 ...
- Nginx 负载均衡原理简介与负载均衡配置详解
Nginx负载均衡原理简介与负载均衡配置详解 by:授客 QQ:1033553122 测试环境 nginx-1.10.0 负载均衡原理 客户端向反向代理发送请求,接着反向代理根据某种负载机制 ...
- Login case
第一步:画UI,代码如下: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" ...
- mac下载的excel如果带有超链接,url被转义问题
注释的代码是file开头的,这种链接在mac系统进行跳转url会转义 hyperlink 还有一种就是http这种就可以正常跳转了. String sLink = basePath + "/ ...
- LeetCode题解之Second Minimum Node In a Binary Tree
1.题目描述 2.问题分析 使用set. 3.代码 set<int> s; int findSecondMinimumValue(TreeNode* root) { dfs(root); ...
- python接口测试—get请求(一)
python 做借口测试用到的是requests模块,首先要导入requests库,pip install requests 1.get直接请求方式 以豆瓣网为例: url = 'https://re ...
- Java输出打印工具类封装
在进行Java打印输出,进行查看字段值的时候,觉得每次写了System.out.println之后,正式发布的时候,还得一个个的删掉,太麻烦了,经过别人的指教,做了一个Java的打印输出封装类,只为记 ...