码迷,mamicode.com
首页 > 其他好文 > 详细

存储过程 查询出多条数据批量插入表 增加出库通知单统计按机构分组事件

时间:2017-11-12 11:09:10      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:datetime   date_sub   pre   sele   机构   end   drop   arc   查询   

use wms;


drop table if exists riv_outbound_notice_statistics_groupby_org;
-- 增加出库通知单统计按机构分组表
CREATE TABLE `riv_outbound_notice_statistics_groupby_org` (
`ONSGO_ID` int(10) NOT NULL AUTO_INCREMENT COMMENT ‘主键ID‘,
`BEGIN_TIME` datetime NOT NULL COMMENT ‘统计起始时间‘,
`END_TIME` datetime NOT NULL COMMENT ‘统计结束时间‘,
`ONH_TOTAL_QTY` bigint(19) NOT NULL DEFAULT ‘0‘ COMMENT ‘出库通知单总创建量‘,
`ONH_SHIPED_QTY` bigint(19) NOT NULL DEFAULT ‘0‘ COMMENT ‘出库通知单已发货量‘,
`ORG_ID` int(10) NOT NULL COMMENT ‘机构ID‘,
`ORG_CODE` varchar(40) NOT NULL COMMENT ‘机构代码‘,
`ORG_NAME` varchar(100) NOT NULL COMMENT ‘机构名称‘,
PRIMARY KEY (`ONSGO_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT=‘出库通知单统计按机构分组表‘;


drop procedure if exists pro_outbound_notice_statistics_groupby_org;
-- 增加出库通知单统计按机构分组函数
delimiter //
create procedure `pro_outbound_notice_statistics_groupby_org`()
begin


DECLARE curtime VARCHAR(50);
DECLARE pretime VARCHAR(50);


set curtime = CONCAT(DATE_FORMAT(now(),‘%Y-%m-%d ‘),‘00:00:00‘);
set pretime = date_sub(curtime, interval 1 day);



insert into riv_outbound_notice_statistics_groupby_org(`BEGIN_TIME`, `END_TIME`,`ONH_TOTAL_QTY`, `ONH_SHIPED_QTY`,`ORG_ID`,`ORG_CODE`, `ORG_NAME`)
select
pretime,
curtime,
tab.totalQty,
tab2.shipedQty,
tab.orgId,
org.ORG_CODE,
org.ORG_NAME
FROM
(
SELECT
count(1) AS totalQty,
ONH_ORG_ID AS orgId
FROM
riv_outbound_notice_h onh
WHERE
onh.CREATE_TIME < curtime
AND onh.CREATE_TIME >= pretime
GROUP BY
onh.ONH_ORG_ID
) tab
LEFT JOIN (
SELECT
count(1) AS shipedQty,
ONH_ORG_ID AS orgId
FROM
riv_outbound_notice_h onh
WHERE
onh.ONH_STATUS = 90
AND onh.ONH_SHIP_DATETIME < curtime
AND onh.ONH_SHIP_DATETIME >= pretime
GROUP BY
onh.ONH_ORG_ID
) tab2 ON tab.orgId = tab2.orgId
INNER JOIN riv_organization org ON tab.orgId = org.ORG_ID;

end//
delimiter ;


drop event if exists event_auto_outbound_notice_statistics_groupby_org;
set global event_scheduler=‘on‘;
-- 增加出库通知单统计按机构分组事件
CREATE EVENT `event_auto_outbound_notice_statistics_groupby_org`
ON SCHEDULE EVERY 1 DAY STARTS ‘2017-11-10 00:00:00‘ ENDS ‘2018-01-01 00:00:00‘
ON COMPLETION NOT PRESERVE
ENABLE
DO
CALL pro_outbound_notice_statistics_groupby_org();

存储过程 查询出多条数据批量插入表 增加出库通知单统计按机构分组事件

标签:datetime   date_sub   pre   sele   机构   end   drop   arc   查询   

原文地址:http://www.cnblogs.com/tonggc1668/p/7820817.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!