BEGIN
declare a1 int default 0;#第一次循环的循环变量
declare a2 int default 0;
declare b1 int default 0;
declare b2 int default 0;
declare c1 int default 0;
declare c2 int default 0;
declare d1 int default 0;
declare d2 int default 0;
declare days int default 0;#当月的第几天
declare ids3 varchar(50);
declare ids varchar(32);
declare ids1 varchar(32);
declare ids2 varchar(32);
declare workno1 varchar(32);
declare name1 varchar(32);
declare amount double default 0;
declare description1 varchar(200);
declare description2 varchar(200);
declare description3 varchar(200); #清空表xhj_sale_zmm关于昨天的数据
DELETE FROM xhj_sale_zmm1 WHERE types_time = '03';
#删除缓存中存在的临时表
drop table IF EXISTS tmpTable;
drop table IF EXISTS ordermain1;
drop table IF EXISTS ordermain2; # 创建临时表– 不存在则创建临时表
create temporary table if not exists tmpTable
(
#id varchar(50),
#id bigint(32) unsigned NOT NULL auto_increment primary key,# 主键id
ranking int,# 排名
names1 varchar(32),# 销售员姓名
number varchar(32),# 工号
dept varchar(500),# 部门描述
deptid varchar(50),# 部门id
amounts double(32,0),# 金额
types_dept varchar(32),# 部门类别
type_time varchar(50)# 时间类别
);
# 使用前先清空临时表。
truncate TABLE tmpTable;
#创建临时表ordermain1并
create temporary table ordermain1 SELECT id,description FROM t_zmm2 where (parentdepartid is null or parentdepartid = '') and status = '0'; create temporary table ordermain2 (SELECT @rownum:=@rownum+1 AS rownum1, ordermain1.* FROM (SELECT @rownum:=0) r, ordermain1);
#查询临时表的总行数
select max(o.rownum1) into a1 from ordermain2 o;
set a2 = 1;
while(a2 <= a1) DO
SELECT id,description into ids,description1 from ordermain2 where rownum1 = a2;
if ids is not null and ids != '' then
drop table IF EXISTS table1;
drop table IF EXISTS table11;
drop table IF EXISTS table2;
drop table IF EXISTS table3;
#创建临时表单1
SELECT * into days from ((SELECT DAYOFMONTH(NOW())) as b);#查询当前是本月第几天;
#判断是否为当月第一天
if days = 1 then
create temporary table table1 SELECT u.realname,a.workno,sum((m.performance_amount * a.scale * 0.01)) as a,'03' as day,'01' as bumen FROM
xhj_sale_zmm3 a LEFT JOIN xhj_sale_zmm1 m on a.oid=m.id LEFT JOIN t_zmm4 u on a.workno = u.username LEFT JOIN t_s_zmm5 o on u.id = o.user_id LEFT JOIN t_zmm2 d on o.org_id = d.id
where d.depart_order like CONCAT('%',ids,'%') and m.confirm_time between (select CONCAT( DATE_FORMAT((SELECT DATE_ADD(NOW(),INTERVAL -1 MONTH)), "%Y-%m"),'-01 00:00:00')) and (select CONCAT(DATE_SUB(curdate(),INTERVAL 1 DAY),' 23:59:59')) and
m.order_time between (select CONCAT( DATE_FORMAT((SELECT DATE_ADD(NOW(),INTERVAL -1 MONTH)), "%Y-%m"),'-01 00:00:00')) and (select CONCAT(DATE_SUB(curdate(),INTERVAL 0 DAY),' 23:59:59')) and m.status not in ('1','3') group by a.workno order by a DESC limit 10;
#添加名次
create temporary table table11 (SELECT @rownum:=@rownum+1 AS rownum11, table1.* FROM (SELECT @rownum:=0) r, table1);
#把得到的数据插入到临时表单中
#SELECT *,a2 as aa FROM table11;
INSERT INTO tmpTable(ranking,names1,number,dept,deptid,amounts,types_dept,type_time)
SELECT rownum11,realname,workno,description1,ids,a,bumen,day FROM table11;
end if;
if days != 1 then
create temporary table table1 SELECT u.realname,a.workno,sum((m.performance_amount * a.scale * 0.01)) as a,'03' as day,'01' as bumen FROM
xhj_sale_zmm3 a LEFT JOIN xhj_sale_zmm1 m on a.oid=m.id LEFT JOIN t_zmm4 u on a.workno = u.username LEFT JOIN t_s_zmm5 o on u.id = o.user_id LEFT JOIN t_zmm2 d on o.org_id = d.id
where d.depart_order like CONCAT('%',ids,'%') and m.confirm_time between (select CONCAT( DATE_FORMAT(now(), "%Y-%m"),'-01 00:00:00')) and (select CONCAT(DATE_SUB(curdate(),INTERVAL 1 DAY),' 23:59:59')) and
m.order_time between (select CONCAT( DATE_FORMAT(now(), "%Y-%m"),'-01 00:00:00')) and (select CONCAT(DATE_SUB(curdate(),INTERVAL 0 DAY),' 23:59:59')) and m.status not in ('1','3') group by a.workno order by a DESC limit 10;
#添加名次
create temporary table table11 (SELECT @rownum:=@rownum+1 AS rownum11, table1.* FROM (SELECT @rownum:=0) r, table1);
#把得到的数据插入到临时表单中
#SELECT *,a2 as aa FROM table11;
INSERT INTO tmpTable(ranking,names1,number,dept,deptid,amounts,types_dept,type_time)
SELECT rownum11,realname,workno,description1,ids,a,bumen,day FROM table11;
end if;
#创建临时表table2---获取二级事业部门id
create temporary table table2 SELECT id,description FROM t_zmm2 where LENGTH(depart_order) = 64 and fax is not null and fax != '' and status = '0' and depart_order like CONCAT('%',ids,'%');
create temporary table table3 (SELECT @rownum:=@rownum+1 AS rownum, table2.* FROM (SELECT @rownum:=0) r, table2);
select max(t.rownum) into b1 from table3 t;
set b2 = 1;
while(b2 <= b1) DO
drop table IF EXISTS table21;
drop table IF EXISTS table4;
drop table IF EXISTS table5;
drop table IF EXISTS table7;
SELECT id,description into ids1,description2 from table3 where rownum = b2;
#SELECT ids1;
if days = 1 then
create temporary table table7 SELECT u.realname,a.workno,sum((m.performance_amount * a.scale * 0.01)) as a,'03' as day,'02' as bumen FROM
xhj_sale_zmm3 a LEFT JOIN xhj_sale_zmm1 m on a.oid=m.id LEFT JOIN t_zmm4 u on a.workno = u.username LEFT JOIN t_zmm5 o on u.id = o.user_id LEFT JOIN t_zmm2 d on o.org_id = d.id
where d.depart_order like CONCAT('%',ids1,'%') and m.confirm_time between (select CONCAT( DATE_FORMAT(now(), "%Y-%m"),'-01 00:00:00')) and (select CONCAT(DATE_SUB(curdate(),INTERVAL 1 DAY),' 23:59:59')) and
m.order_time between (select CONCAT( DATE_FORMAT((SELECT DATE_ADD(NOW(),INTERVAL -1 MONTH)), "%Y-%m"),'-01 00:00:00')) and (select CONCAT(DATE_SUB(curdate(),INTERVAL 0 DAY),' 23:59:59')) and m.status not in ('1','3') group by a.workno order by a DESC limit 5;
#添加名次
create temporary table table21 (SELECT @rownum:=@rownum+1 AS rownum21, table7.* FROM (SELECT @rownum:=0) r, table7);
#把得到的数据插入到临时表单中
INSERT INTO tmpTable(ranking,names1,number,dept,deptid,amounts,types_dept,type_time)
SELECT rownum21,realname,workno,description2,ids1,a,bumen,day FROM table21;
end if;
if days != 1 then
create temporary table table7 SELECT u.realname,a.workno,sum((m.performance_amount * a.scale * 0.01)) as a,'03' as day,'02' as bumen FROM
xhj_sale_zmm3 a LEFT JOIN xhj_sale_zmm1 m on a.oid=m.id LEFT JOIN t_zmm4 u on a.workno = u.username LEFT JOIN t_zmm5 o on u.id = o.user_id LEFT JOIN t_zmm2 d on o.org_id = d.id
where d.depart_order like CONCAT('%',ids1,'%') and m.confirm_time between (select CONCAT( DATE_FORMAT(now(), "%Y-%m"),'-01 00:00:00')) and (select CONCAT(DATE_SUB(curdate(),INTERVAL 1 DAY),' 23:59:59')) and
m.order_time between (select CONCAT( DATE_FORMAT(now(), "%Y-%m"),'-01 00:00:00')) and (select CONCAT(DATE_SUB(curdate(),INTERVAL 0 DAY),' 23:59:59')) and m.status not in ('1','3') group by a.workno order by a DESC limit 5;
#添加名次
create temporary table table21 (SELECT @rownum:=@rownum+1 AS rownum21, table7.* FROM (SELECT @rownum:=0) r, table7);
#把得到的数据插入到临时表单中
INSERT INTO tmpTable(ranking,names1,number,dept,deptid,amounts,types_dept,type_time)
SELECT rownum21,realname,workno,description2,ids1,a,bumen,day FROM table21;
end if;
#创建临时表table5---获取三级部门id
create temporary table table4 SELECT id,description FROM t_zmm2 where LENGTH(depart_order) = 96 and status = '0' and depart_order like CONCAT('%',ids1,'%');
create temporary table table5 (SELECT @rownum:=@rownum+1 AS rownum3, table4.* FROM (SELECT @rownum:=0) r, table4);
select max(t2.rownum3) into c1 from table5 t2;
set c2 = 1;
while(c2 <= c1) DO
drop table IF EXISTS table31;
drop table IF EXISTS table6;
#得到三级部门前三名信息
if days = 1 then
SELECT id,description into ids2,description3 from table5 where rownum3 = c2;
create temporary table table6 SELECT u.realname,a.workno,sum((m.performance_amount * a.scale * 0.01)) as a,'03' as day,'03' as bumen FROM
xhj_sale_zmm3 a LEFT JOIN xhj_sale_zmm1 m on a.oid=m.id LEFT JOIN t_zmm4 u on a.workno = u.username LEFT JOIN t_zmm5 o on u.id = o.user_id LEFT JOIN t_zmm2 d on o.org_id = d.id
where d.depart_order like CONCAT('%',ids2,'%') and m.confirm_time between (select CONCAT( DATE_FORMAT(now(), "%Y-%m"),'-01 00:00:00')) and (select CONCAT(DATE_SUB(curdate(),INTERVAL 1 DAY),' 23:59:59')) and
m.order_time between (select CONCAT( DATE_FORMAT((SELECT DATE_ADD(NOW(),INTERVAL -1 MONTH)), "%Y-%m"),'-01 00:00:00')) and (select CONCAT(DATE_SUB(curdate(),INTERVAL 0 DAY),' 23:59:59')) and m.status not in ('1','3') group by a.workno order by a DESC limit 3;
#添加名次
create temporary table table31 (SELECT @rownum:=@rownum+1 AS rownum31, table6.* FROM (SELECT @rownum:=0) r, table6);
#把得到的数据插入到临时表单中
INSERT INTO tmpTable(ranking,names1,number,dept,deptid,amounts,types_dept,type_time)
SELECT rownum31,realname,workno,description3,ids2,a,bumen,day FROM table31; end if;
if days != 1 then
SELECT id,description into ids2,description3 from table5 where rownum3 = c2;
create temporary table table6 SELECT u.realname,a.workno,sum((m.performance_amount * a.scale * 0.01)) as a,'03' as day,'03' as bumen FROM
xhj_sale_zmm3 a LEFT JOIN xhj_sale_zmm1 m on a.oid=m.id LEFT JOIN t_zmm4 u on a.workno = u.username LEFT JOIN t_zmm5 o on u.id = o.user_id LEFT JOIN t_zmm2 d on o.org_id = d.id
where d.depart_order like CONCAT('%',ids2,'%') and m.confirm_time between (select CONCAT( DATE_FORMAT(now(), "%Y-%m"),'-01 00:00:00')) and (select CONCAT(DATE_SUB(curdate(),INTERVAL 1 DAY),' 23:59:59')) and
m.order_time between (select CONCAT( DATE_FORMAT(now(), "%Y-%m"),'-01 00:00:00')) and (select CONCAT(DATE_SUB(curdate(),INTERVAL 0 DAY),' 23:59:59')) and m.status not in ('1','3') group by a.workno order by a DESC limit 3;
#添加名次
create temporary table table31 (SELECT @rownum:=@rownum+1 AS rownum31, table6.* FROM (SELECT @rownum:=0) r, table6);
#把得到的数据插入到临时表单中
INSERT INTO tmpTable(ranking,names1,number,dept,deptid,amounts,types_dept,type_time)
SELECT rownum31,realname,workno,description3,ids2,a,bumen,day FROM table31;
end if;
drop table IF EXISTS table31;
drop table IF EXISTS table6;
set c2 = c2 + 1;
end while;
drop table IF EXISTS table21;
drop table IF EXISTS table4;
drop table IF EXISTS table5;
drop table IF EXISTS table7;
set b2 = b2 + 1;
end while;
drop table IF EXISTS table1;
drop table IF EXISTS table11;
drop table IF EXISTS table2;
drop table IF EXISTS table3;
end if;
set a2 = a2 + 1;
end while; #删除缓存中存在的临时表 INSERT INTO xhj_sale_zmm(ranking,name,number,dept,deptid,amount,types_dept,types_time)
SELECT ranking,names1,number,dept,deptid,amounts,types_dept,type_time FROM tmpTable;
SELECT * FROM xhj_sale_zmm;
drop table IF EXISTS ordermain1;
drop table IF EXISTS ordermain2;
drop table IF EXISTS tmpTable;
END

MySQL函数转储存(当前月数据同步)的更多相关文章

  1. MySQL函数GROUP_CONCAT() 实现多条数据合并

    group_concat()会计算哪些行属于同一组,将属于同一组的列显示出来,group by指定的列进行分组. 例如: -- 根据物流订单id查询所有物流订单,车源订单,车辆信息(多条数据合并为一条 ...

  2. MySQL 关联表批量修改(数据同步)

    update table1 t1 ,table2 t2 set t1.field1 = t2.field2 where t1.id = t2.id

  3. 通过Gearman实现MySQL到Redis的数据同步

    对于变化频率非常快的数据来说,如果还选择传统的静态缓存方式(Memocached.File System等)展示数据,可能在缓存的存取上会有很大的开销,并不能很好的满足需要,而Redis这样基于内存的 ...

  4. redis作为mysql的缓存服务器(读写分离,通过mysql触发器实现数据同步)

    一.redis简介Redis是一个key-value存储系统.和Memcached类似,为了保证效率,数据都是缓存在内存中.区别的是redis会周期性的把更新的数据写入磁盘或者把修改操作写入追加的记录 ...

  5. Memcached与MySQL数据同步

    1.介绍 在生产环境中,我们经常使用MySQL作为应用的数据库.但是随着用户的增多数据量的增大,我们将会自然而然的选择Memcached作为缓存数据库,从而减小MySQL的压力.但是memcached ...

  6. JAVA通过Gearman实现MySQL到Redis的数据同步(异步复制)

    MySQL到Redis数据复制方案 无论MySQL还是Redis,自身都带有数据同步的机制,像比较常用的 MySQL的Master/Slave模式 ,就是由Slave端分析Master的binlog来 ...

  7. 转载:mysql数据同步redis

    from: http://www.cnblogs.com/zhxilin/archive/2016/09/30/5923671.html 在服务端开发过程中,一般会使用MySQL等关系型数据库作为最终 ...

  8. redis和mySql的数据同步的解析

    1.同步MySQL数据到Redis (1) 在redis数据库设置缓存时间,当该条数据缓存时间过期之后自动释放,去数据库进行重新查询,但这样的话,我们放在缓存中的数据对数据的一致性要求不是很高才能放入 ...

  9. Redis和MySQL数据同步及Redis使用场景

    1.同步MySQL数据到Redis (1) 在redis数据库设置缓存时间,当该条数据缓存时间过期之后自动释放,去数据库进行重新查询,但这样的话,我们放在缓存中的数据对数据的一致性要求不是很高才能放入 ...

随机推荐

  1. intellij idea 相关设置

    IDEA这么高端的工具之前只是断断续续使用了一下,因为项目的开发都是在eclipse上,每次学习IDEA的使用都得上网搜索半天,今天自己整理一下,方便以后查阅. IDEA版本15.0.4 字体 界面字 ...

  2. Json解析注解 ---@SerializedName

    项目前后端交互采用的是JSON传输,因为前后端沟通不多,经常造成数据格式不统一:在不想后台改动太多下,需要使用@SerializedName解决这个问题 解析数据使用的是Gson解析的json数据 @ ...

  3. python学习笔记----random

    import random import string # 随机整数: print random.randint(1,50) >>> print(random.randint(1,5 ...

  4. ThreadLocal总结

    一.问题抛出 SimpleDateFormat是非线程安全的,在多线程情况下会遇见问题: public static void main(String[] args) { ExecutorServic ...

  5. ASE存储过程和IQ存储过程的常见区别(附例子)

    ASE存储过程和IQ存储过程的常见区别(附例子) 1 存储过程简介 存储过程(Stored Procedure)是为了完成特定的功能而汇集成一组的SQL语句集,并为该组SQL语句命名.经编译后存储在S ...

  6. python拼接字符串方法汇总

    python拼接字符串一般有以下几种方法: 1.直接通过(+)操作符拼接 s = 'Hello'+' '+'World'+'!' print(s) 输出结果:Hello World! 这种方式最常用. ...

  7. wget -r -c -nd

    wget -r -c -nd -r 递归下载 -c 断点续传 -nd 不创建目录, wget默认会创建一个目录 -l1 (L one) 递归一层,只下载指定文件夹中的内容, 不下载下一级目录中的.–n ...

  8. VS2015环境下的提示语法错误:编号的预期结尾后有多余文本(extra text after expected end of number)

    当工程中有扩展CListCtrl的类代码,并且有自绘单元格的操作, ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnNMCustomdraw) 编辑器语法智能提示就会对这个宏定义 ...

  9. CentOS6.3上搭建expect无交互开发环境

    1.背景 在面向shell编程时对于需要交互的场合则必须通过人工来干预,而对于这种方式是违反无人职守的原则:现在expect就解决了这个问题, Expect是一个免费的编程工具语言,用来实现自动和交互 ...

  10. Linux shell 批量运行jmeter脚本

    第一版,这些代码有点问题,需要继续更改 #!/bin/bash jmxpath= reportpath= timestamp=$(date +%Y%m%d_%H%M%S) echo timestamp ...