Postgre存储过程写法

declare
tempSql text;
store_refcursor refcursor; 
id BIGINT;
name VARCHAR;
exist int;

BEGIN
tempSql :='select id as storeid,storename as storename  
from kx_kq_store 
where updatetime BETWEEN '''||begintime||''' AND '''||endtime||'''and platstatus=1';

raise notice 'tempSql is : %',tempSql;

	-- 游标遍历纪录 to_char(l.createtime,''yyyy-MM-dd'') as date
	open store_refcursor for execute tempSql;
		loop 
			fetch store_refcursor into id,name;
					if found then 
			
						-- 对于数据库里有的数据就做增量修改,没有的数据就做新增
						select count(*) from bi_dim_store where storeid=id into exist;
							if exist > 0 
								then
									update bi_dim_store set storename = name,platupdatetime = now()
									where storeid=id;
							else
								  insert into bi_dim_store(storeid,storename,platcreatetime,platupdatetime) 
                                                 values (id,name,now(),now());
							end if;
					else 
				     exit;
				  end if;
		end loop;
	close store_refcursor;

END

下面是一个比较复杂的存储过程:

declare
deleteSql text;
totalSql text;
worksetmemberSql text;
tempworkdateSql text;
plantempSql text;
plandatenumtempSql text;
actualdatenumtempSql text;
othersSql text;
nianjiatempSql text;
tiaoxiujiatempSql text;
bingjiatempSql text;
shijiatempSql text;
hunjiatempSql text;
chanjiatempSql text;
peichanjiatempSql text;
selectTempSql text;
insertSql text;
begin	
  
	if currdate is null OR currdate  = '' THEN
			currdate := '' || now();
	end if;
	
	deleteSql:= 'delete from kx_kq_monthreport_quick where reportmonth = to_char('''|| currdate  ||'''::timestamp,''YYYY-MM'')';

  raise notice 'deleteSql is : %',deleteSql;
	
	execute deleteSql;


	--查询出所有人员,营销区域,年月
	totalSql = 'select distinct m.userid,to_char('''|| currdate  ||'''::timestamp,''YYYY-MM'') as reportmonth,p.orgstructid  as saleareaid 
	from kx_kq_signmiddleresult m
	inner join pl_userinfo p on p.userinfoid=m.userid
	where m.recorddate>=to_timestamp((to_char('''|| currdate  ||'''::timestamp,''YYYY-MM'')||''-01''),''YYYY-MM-DD'')
	and m.recorddate<to_timestamp((to_char(( '''|| currdate  ||'''::timestamp + interval ''1 month''),''YYYY-MM'')||''-01''),''YYYY-MM-DD'')';

	--获取人员考勤组
	 worksetmemberSql = 'select  case when qk.worksetid is null then 3001 else qk.worksetid end as worksetid,pl.userinfoid as userid  from pl_userinfo pl
	left join kx_qk_worksetmember qk on qk.userid=pl.userinfoid';


	--临时表,查询的结果依次为:人员,考勤组,工作日,当天是否出勤,当天应出勤工时,当天实出勤工时(暂时不计算),
	--迟到次数,迟到时间,早退次数,早退时间,缺卡次数,
	 tempworkdateSql = 'select m.userid,m.recorddate,
	sum(case when m.signdatetime is not null or m.signoffdatetime is not null then 1 else 0 end ) as issign ,
	sum(m.timesegmenthours) as planworkhours,
	sum(case when (m.signdatetime is null or m.signoffdatetime is null)or (m.signdatetime>=m.signoffdatetime) then 0  
			when m.begintime is null or m.endtime is null then  ((extract(epoch from m.signoffdatetime)-extract(epoch from m.signdatetime)))/3600
			when m.signdatetime<=m.begintime and m.signoffdatetime>=m.endtime then ((extract(epoch from m.signoffdatetime)-extract(epoch from m.signdatetime))-(extract(epoch from m.endtime)-extract(epoch from m.begintime)))/3600
			when m.signdatetime>=m.begintime and m.signoffdatetime<=m.endtime then 0
			when (m.signdatetime<=m.begintime and m.signoffdatetime<=m.begintime) or (m.signdatetime>=m.endtime and m.signoffdatetime>=m.endtime)  then (extract(epoch from m.signoffdatetime)-extract(epoch from m.signdatetime))/3600
			when m.signdatetime>=m.begintime and m.signoffdatetime>=m.endtime then (extract(epoch from m.signoffdatetime)-extract(epoch from m.endtime))/3600
			when m.signdatetime<=m.begintime and m.signoffdatetime<=m.endtime then (extract(epoch from m.begintime)-extract(epoch from m.signdatetime))/3600
			else 0 
	end )  as actualhours,
	sum(case when m.signstatus=''1'' then 1   else 0 end) as latenum, 
	sum(m.signduration) as signduration,
	sum(case when m.signoffstatus=''2'' then 1  else 0 end) as leaveearlynum, 
	sum(m.signoffduration) as leaveearlyhours,
	sum(case when m.signstatus=''4'' then 1   else 0 end) as notsignnum1, 
	sum(case when m.signoffstatus=''4'' then 1   else 0 end) as notsignnum2,
	sum(case when m.signstatus=''0'' then 1   else 0 end) as normal1,
	sum(case when m.signoffstatus=''0'' then 1  else 0 end) as normal2
	from kx_kq_signmiddleresult m
	where m.recorddate>= to_timestamp((to_char('''|| currdate  ||'''::timestamp,''YYYY-MM'')||''-01''),''YYYY-MM-DD'')
	and m.recorddate<to_timestamp((to_char(( '''|| currdate  ||'''::timestamp + interval ''1 month''),''YYYY-MM'')||''-01''),''YYYY-MM-DD'')
	and m.isworkdate=''1'' 
	group by m.userid,m.recorddate';


	--其他指标:应出勤工时,实出勤工时,迟到次数,迟到时间,早退次数,早退时间,缺卡次数,
	 othersSql = 'select t.userid,t.reportmonth,t.saleareaid,
	case when sum(m.planworkhours)=0 then 0 else  round(sum(m.planworkhours)::numeric,2) end as planworkhours,
	case when sum(m.actualhours)=0 then 0 else round(sum(m.actualhours)::numeric,2) end as actualhours,
	sum(m.latenum) as latenum, 
	case when sum(m.signduration)=0 then 0 else round(sum(m.signduration)::numeric,2) end as latehours, 
	sum(m.leaveearlynum) as leaveearlynum, 
	case when sum(m.leaveearlyhours)=0 then 0 else  round(sum(m.leaveearlyhours)::numeric,2) end as leaveearlyhours, 
	sum(m.notsignnum1+m.notsignnum2) as notsignnum ,
	sum(m.normal1+m.normal2) as normalsign
	from (' || totalSql || ') as t
	left join (' || tempworkdateSql || ') as m on m.userid=t.userid 
	group by t.userid,t.reportmonth,t.saleareaid';

	--年假
	 nianjiatempSql = 'select l.usercode as userid, sum(l.datespan) as nianjia
	from kx_leave l
	where l.approvalstatus=''审批通过''  
	and l.leavetype=895839991220015104
	and ((l.starttime>=to_timestamp((to_char('''|| currdate  ||'''::timestamp,''YYYY-MM'')||''-01''),''YYYY-MM-DD'')
	and l.starttime< to_timestamp((to_char(( '''|| currdate  ||'''::timestamp + interval ''1 month''),''YYYY-MM'')||''-01''),''YYYY-MM-DD'')) 
	or(l.endtime>=to_timestamp((to_char('''|| currdate  ||'''::timestamp,''YYYY-MM'')||''-01''),''YYYY-MM-DD'') 
	and l.endtime< to_timestamp((to_char(( '''|| currdate  ||'''::timestamp + interval ''1 month''),''YYYY-MM'')||''-01''),''YYYY-MM-DD''))
	)
	group by l.usercode';


	--调休假
	 tiaoxiujiatempSql = 'select l.usercode as userid, sum(l.datespan) as tiaoxiujia
	from kx_leave l
	where l.approvalstatus=''审批通过''  
	and l.leavetype=895840294887624704
	and ((l.starttime>=to_timestamp((to_char('''|| currdate  ||'''::timestamp,''YYYY-MM'')||''-01''),''YYYY-MM-DD'')
	and l.starttime< to_timestamp((to_char(( '''|| currdate  ||'''::timestamp + interval ''1 month''),''YYYY-MM'')||''-01''),''YYYY-MM-DD'')) 
	or(l.endtime>=to_timestamp((to_char('''|| currdate  ||'''::timestamp,''YYYY-MM'')||''-01''),''YYYY-MM-DD'') 
	and l.endtime< to_timestamp((to_char(( '''|| currdate  ||'''::timestamp + interval ''1 month''),''YYYY-MM'')||''-01''),''YYYY-MM-DD''))
	)
	group by l.usercode';


	--病假
	 bingjiatempSql = 'select l.usercode as userid, sum(l.datespan) as bingjia
	from kx_leave l
	where l.approvalstatus=''审批通过''  
	and l.leavetype=895840420637052928
	and ((l.starttime>=to_timestamp((to_char('''|| currdate  ||'''::timestamp,''YYYY-MM'')||''-01''),''YYYY-MM-DD'')
	and l.starttime< to_timestamp((to_char(( '''|| currdate  ||'''::timestamp + interval ''1 month''),''YYYY-MM'')||''-01''),''YYYY-MM-DD'')) 
	or(l.endtime>=to_timestamp((to_char('''|| currdate  ||'''::timestamp,''YYYY-MM'')||''-01''),''YYYY-MM-DD'') 
	and l.endtime< to_timestamp((to_char(( '''|| currdate  ||'''::timestamp + interval ''1 month''),''YYYY-MM'')||''-01''),''YYYY-MM-DD''))
	)
	group by l.usercode';


	--事假
	 shijiatempSql = 'select l.usercode as userid, sum(l.datespan) as shijia
	from kx_leave l
	where l.approvalstatus=''审批通过''  
	and l.leavetype=895840335027113984
	and ((l.starttime>=to_timestamp((to_char('''|| currdate  ||'''::timestamp,''YYYY-MM'')||''-01''),''YYYY-MM-DD'')
	and l.starttime< to_timestamp((to_char(( '''|| currdate  ||'''::timestamp + interval ''1 month''),''YYYY-MM'')||''-01''),''YYYY-MM-DD'')) 
	or(l.endtime>=to_timestamp((to_char('''|| currdate  ||'''::timestamp,''YYYY-MM'')||''-01''),''YYYY-MM-DD'') 
	and l.endtime< to_timestamp((to_char(( '''|| currdate  ||'''::timestamp + interval ''1 month''),''YYYY-MM'')||''-01''),''YYYY-MM-DD''))
	)
	group by l.usercode' 
	;


	--婚假
	 hunjiatempSql = 'select l.usercode as userid, sum(l.datespan) as hunjia
	from kx_leave l
	where l.approvalstatus=''审批通过''  
	and l.leavetype=895840738766622720
	and ((l.starttime>=to_timestamp((to_char('''|| currdate  ||'''::timestamp,''YYYY-MM'')||''-01''),''YYYY-MM-DD'')
	and l.starttime< to_timestamp((to_char(( '''|| currdate  ||'''::timestamp + interval ''1 month''),''YYYY-MM'')||''-01''),''YYYY-MM-DD'')) 
	or(l.endtime>=to_timestamp((to_char('''|| currdate  ||'''::timestamp,''YYYY-MM'')||''-01''),''YYYY-MM-DD'') 
	and l.endtime< to_timestamp((to_char(( '''|| currdate  ||'''::timestamp + interval ''1 month''),''YYYY-MM'')||''-01''),''YYYY-MM-DD''))
	)
	group by l.usercode';


	--产假
	 chanjiatempSql = 'select l.usercode as userid, sum(l.datespan) as chanjia
	from kx_leave l
	where l.approvalstatus=''审批通过''  
	and l.leavetype=895840767371776000
	and ((l.starttime>=to_timestamp((to_char('''|| currdate  ||'''::timestamp,''YYYY-MM'')||''-01''),''YYYY-MM-DD'')
	and l.starttime< to_timestamp((to_char(( '''|| currdate  ||'''::timestamp + interval ''1 month''),''YYYY-MM'')||''-01''),''YYYY-MM-DD'')) 
	or(l.endtime>=to_timestamp((to_char('''|| currdate  ||'''::timestamp,''YYYY-MM'')||''-01''),''YYYY-MM-DD'') 
	and l.endtime< to_timestamp((to_char(( '''|| currdate  ||'''::timestamp + interval ''1 month''),''YYYY-MM'')||''-01''),''YYYY-MM-DD''))
	)
	group by l.usercode';


	--陪产假
	 peichanjiatempSql = 'select l.usercode as userid, sum(l.datespan) as peichanjia
	from kx_leave l
	where l.approvalstatus=''审批通过''  
	and l.leavetype=895840799688888320
	and ((l.starttime>=to_timestamp((to_char('''|| currdate  ||'''::timestamp,''YYYY-MM'')||''-01''),''YYYY-MM-DD'')
	and l.starttime< to_timestamp((to_char(( '''|| currdate  ||'''::timestamp + interval ''1 month''),''YYYY-MM'')||''-01''),''YYYY-MM-DD'')) 
	or(l.endtime>=to_timestamp((to_char('''|| currdate  ||'''::timestamp,''YYYY-MM'')||''-01''),''YYYY-MM-DD'') 
	and l.endtime< to_timestamp((to_char(( '''|| currdate  ||'''::timestamp + interval ''1 month''),''YYYY-MM'')||''-01''),''YYYY-MM-DD''))
	)
	group by l.usercode';



	selectTempSql = 'SELECT t.userid,t.reportmonth,t.saleareaid,o.latenum,o.leaveearlynum,o.notsignnum,o.normalsign,
						COALESCE (t1.nianjia, 0),COALESCE (t2.tiaoxiujia, 0),COALESCE (t3.bingjia, 0),COALESCE (t4.shijia, 0),COALESCE (t5.hunjia, 0),COALESCE (t6.chanjia, 0),COALESCE (t7.peichanjia, 0)
									 FROM (' || totalSql || ') as t
						left join (' || othersSql || ') as o on o.userid=t.userid and o.reportmonth=t.reportmonth and o.saleareaid=t.saleareaid 
						left join (' || nianjiatempSql || ') as t1 on t1.userid=t.userid
						left join (' || tiaoxiujiatempSql || ') as t2 on t2.userid=t.userid
						left join (' || bingjiatempSql || ') as t3 on t3.userid=t.userid 
						left join (' || shijiatempSql || ') as t4 on t4.userid=t.userid 
						left join (' || hunjiatempSql || ') as t5 on t5.userid=t.userid  
						left join (' || chanjiatempSql || ') as t6 on t6.userid=t.userid 
						left join (' || peichanjiatempSql || ') as t7 on t7.userid=t.userid';

	insertSql := 'INSERT INTO kx_kq_monthreport_quick(userid,reportmonth,saleareaid,latenum,leaveearlynum,notsignnum,normalsign,
               nianjia,tiaoxiujia,bingjia,shijia,hunjia,chanjia,peichanjia) '|| selectTempSql ;
		
	execute insertSql;

  -- 更新跨月审批问题

	perform  f_update_leave_type_onemonth_time_quick();

end;

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PostgreSQL是以加州大学伯克利分校计算机系开发的POSTGRES,现在已经更名为PostgreSQL. PostgreSQL支持大部分SQL标准并且提供了许多其它现代特性:复杂查询、外键、触发器、视图、事务完整性等。PostgreSQL 是一个免费的对象-关系数据库服务器(数据库管理系统),它在灵活的 BSD-风格许可证下发行。它提供了相对其他开放源代码数据库系统(比如 MySQL 和 Firebird),和专有系统(比如 Oracle、Sybase、IBM 的 DB2 和 Microsoft SQL Server)之外的另一种选择。事实上, PostgreSQL 的特性覆盖了 SQL-2/SQL-92 和 SQL-3/SQL-99,首先,它包括了可以说是目前世界上最丰富的数据类型的支持,其中有些数据类型可以说连商业数据库都不具备, 比如 IP 类型和几何类型等;其次,PostgreSQL 是全功能的自由软件数据库,很长时间以来,PostgreSQL 是唯一支持事务、子查询、多版本并行控制系统(MVCC)、数据完整性检查等特性的唯一的一种自由软件的数据库管理系统。 Inprise 的 InterBase 以及SAP等厂商将其原先专有软件开放为自由软件之后才打破了这个唯一。最后,PostgreSQL拥有一支非常活跃的开发队伍,而且在许多黑客的努力下,PostgreSQL 的质量日益提高。从技术角度来讲,PostgreSQL 采用的是比较经典的C/S(client/server)结构,也就是一个客户端对应一个服务器端守护进程的模式,这个守护进程分析客户端来的查询请求,生成规划树,进行数据检索并最终把结果格式化输出后返回给客户端。为了便于客户端的程序的编写,由数据库服务器提供了统一的客户端 C 接口。而不同的客户端接口都是源自这个 C 接口,比如ODBC,JDBC,Python,Perl,Tcl,C/C++,ESQL等, 同时也要指出的是,PostgreSQL 对接口的支持也是非常丰富的,几乎支持所有类型的数据库客户端接口。这一点也可以说是 PostgreSQL 一大优点。本课程作为PostgreSQL数据库管理一,主要讲解以下内容: 1.     PostgreSQL 存储过程基本知识2.     PostgreSQL 用户自定义函数3.     PostgreSQL 控制结构4.     PostgreSQL 游标和存储过程5.     PostgreSQL 索引6.     PostgreSQL 视图7.     PostgreSQL 触发器8.     PostgreSQL 角色、备份和还原9.     PostgreSQL 表空间管理
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值