MySQL 基于存储过程 实现数据统计按日、周、月份统计模板

 

存储过程developer_count 是根据传入参数searchType 决定是使用那种查询方式,本存储过程中包含的其他的参数是{起始时间:startime,结束时间:endtime}

 

[html]  view plain  copy
 
  1. CREATE PROCEDURE developer_count  
  2. (  
  3.   searchType int,  
  4.     startTime varchar(64),  
  5.     endTime varchar(64)  
  6. )  
  7. BEGIN  
  8.     /*定义变量天数*/  
  9.   declare day_num int;  
  10.   
  11.   if searchType = 1 then   
  12.   /*本周数据查询*/  
  13.    select count(d.acct_id),d.acct_old_time from developer d where  1=1 and DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(d.acct_old_time) GROUP BY d.acct_old_time;  
  14.     end if;  
  15.   if searchType = 2 then  
  16.     /*本月数据查询*/  
  17.         select count(d.acct_id),d.acct_old_time from developer d where  1=1 and DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(d.acct_old_time) GROUP BY d.acct_old_time;  
  18.   end if;  
  19.   if searchType = 3 then  
  20.     /*最近三个月数据查询*/  
  21.         select count(d.acct_id), DATE_FORMAT(d.acct_old_time,'%x年-第%v周') as weeks from developer d where  1=1 and DATE_SUB(CURDATE(), INTERVAL 90 DAY) <= date(d.acct_old_time) GROUP BY weeks;  
  22.   end if;  
  23.   if searchType = 4 then  
  24.     /*按月份进行数据统计*/  
  25.         select datediff(startTime, endTime) into day_num;  
  26.         if day_num <=7 then  
  27.                 select count(d.acct_id),d.acct_old_time from developer d where  1=1 and DATE_SUB(CURDATE(), INTERVAL day_num DAY) <= date(d.acct_old_time) GROUP BY d.acct_old_time;  
  28.         end if;  
  29.         if day_num >7  && day_num <= 30 then  
  30.                 select count(d.acct_id),d.acct_old_time from developer d where  1=1 and DATE_SUB(CURDATE(), INTERVAL day_num DAY) <= date(d.acct_old_time) GROUP BY d.acct_old_time;  
  31.         end if;  
  32.         if day_num >30 && day_num <= 90 then  
  33.                 select count(d.acct_id), DATE_FORMAT(d.acct_old_time,'%x年-第%v周') as weeks from developer d where  1=1 and DATE_SUB(CURDATE(), INTERVAL day_num DAY) <= date(d.acct_old_time) GROUP BY weeks;  
  34.         end if;       
  35.     end if;  
  36.   
  37. end;  

调用存储过程方法

 

CALL DEVELOPER_COUNT(1,'2016-06-07','2016-06-16');调用存储过程

转载于:https://www.cnblogs.com/fire909090/p/7102544.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值