SQL对数据进行按月统计或对数据进行按星期统计的实例代码

 对于所有的需求,当你不知道怎么处理的时候,你就先用最简单的方法,或者说的明白一点,用最原始的方法,先实现业务需求再说。  

一、对提现队列数据表“ims_checkout_task”进行汇总统计,按月汇总统计每个月的提现总额,提现总次数。

  1、sql操作如下:

select id
,sum(case when from_unixtime(addtime,'%y-%m') = date_format(date_sub(curdate(), interval 11 month),'%y-%m') and `status` = 1 then money else 0 end) as '0'
,sum(case when from_unixtime(addtime,'%y-%m') = date_format(date_sub(curdate(), interval 10 month),'%y-%m') and `status` = 1 then money else 0 end) as '1'
,sum(case when from_unixtime(addtime,'%y-%m') = date_format(date_sub(curdate(), interval 9 month),'%y-%m') and `status` = 1 then money else 0 end) as '2'
,sum(case when from_unixtime(addtime,'%y-%m') = date_format(date_sub(curdate(), interval 8 month),'%y-%m') and `status` = 1 then money else 0 end) as '3'
,sum(case when from_unixtime(addtime,'%y-%m') = date_format(date_sub(curdate(), interval 7 month),'%y-%m') and `status` = 1 then money else 0 end) as '4'
,sum(case when from_unixtime(addtime,'%y-%m') = date_format(date_sub(curdate(), interval 6 month),'%y-%m') and `status` = 1 then money else 0 end) as '5'
,sum(case when from_unixtime(addtime,'%y-%m') = date_format(date_sub(curdate(), interval 5 month),'%y-%m') and `status` = 1 then money else 0 end) as '6'
,sum(case when from_unixtime(addtime,'%y-%m') = date_format(date_sub(curdate(), interval 4 month),'%y-%m') and `status` = 1 then money else 0 end) as '7'
,sum(case when from_unixtime(addtime,'%y-%m') = date_format(date_sub(curdate(), interval 3 month),'%y-%m') and `status` = 1 then money else 0 end) as '8'
,sum(case when from_unixtime(addtime,'%y-%m') = date_format(date_sub(curdate(), interval 2 month),'%y-%m') and `status` = 1 then money else 0 end) as '9'
,sum(case when from_unixtime(addtime,'%y-%m') = date_format(date_sub(curdate(), interval 1 month),'%y-%m') and `status` = 1 then money else 0 end) as '10'
,sum(case when from_unixtime(addtime,'%y-%m') = date_format(date_sub(curdate(), interval 0 month),'%y-%m') and `status` = 1 then money else 0 end) as '11'
from ims_checkout_task

   2、数据库返回如下:

   3、关键词:case when

//流程控制语句case语法,例如,如果sex字段值为1,则输出男;如果sex值为2,则输出女;否则输出其他
case sex
when '1' then '男'
when '2' then '女'
else '其他' end
//所以上面的sql为,如果条件成立,则输出提现金额money字段,否则输出0.

  时间处理

//对时间戳格式化成 2018-10
from_unixtime(addtime,'%y-%m')
//sql获取当前时间格式 2019-08 ,根据expr值不同,依次获取前一个月1,前两个月2 ···
date_format(date_sub(curdate(), interval 0 month),'%y-%m')
// 函数用于以不同的格式显示日期/时间数据。
date_format(date,format)
//函数从日期减去指定的时间间隔。
date_sub(date,interval expr type)
//函数返回当前的日期。
curdate()

  例如

select now(),curdate(),curtime()

   例如

select dayofweek(now()),weekday(now()),date_format(now(),"%w"),now()

二、对积分订单数据表按周汇总统计订单量,比如今天是周二,返回周一到周二的每天单量汇总数据,依次类推

  1、sql操作如下:

select id
,sum(case when date_format(from_unixtime(pay_time,'%y-%m-%d'),'%w') = 1 and week(date_add(curdate(),interval 6 day),2) = week(date_add(from_unixtime(pay_time,'%y-%m-%d'),interval 6 day),2) and `pay_status` = 1 then 1 else 0 end) as '0'
,sum(case when date_format(from_unixtime(pay_time,'%y-%m-%d'),'%w') = 2 and week(date_add(curdate(),interval 6 day),2) = week(date_add(from_unixtime(pay_time,'%y-%m-%d'),interval 6 day),2) and `pay_status` = 1 then 1 else 0 end) as '1'
,sum(case when date_format(from_unixtime(pay_time,'%y-%m-%d'),'%w') = 3 and week(date_add(curdate(),interval 6 day),2) = week(date_add(from_unixtime(pay_time,'%y-%m-%d'),interval 6 day),2) and `pay_status` = 1 then 1 else 0 end) as '2'
,sum(case when date_format(from_unixtime(pay_time,'%y-%m-%d'),'%w') = 4 and week(date_add(curdate(),interval 6 day),2) = week(date_add(from_unixtime(pay_time,'%y-%m-%d'),interval 6 day),2) and `pay_status` = 1 then 1 else 0 end) as '3'
,sum(case when date_format(from_unixtime(pay_time,'%y-%m-%d'),'%w') = 5 and week(date_add(curdate(),interval 6 day),2) = week(date_add(from_unixtime(pay_time,'%y-%m-%d'),interval 6 day),2) and `pay_status` = 1 then 1 else 0 end) as '4'
,sum(case when date_format(from_unixtime(pay_time,'%y-%m-%d'),'%w') = 6 and week(date_add(curdate(),interval 6 day),2) = week(date_add(from_unixtime(pay_time,'%y-%m-%d'),interval 6 day),2) and `pay_status` = 1 then 1 else 0 end) as '5'
,sum(case when date_format(from_unixtime(pay_time,'%y-%m-%d'),'%w') = 0 and week(date_add(curdate(),interval 6 day),2) = week(date_add(from_unixtime(pay_time,'%y-%m-%d'),interval 6 day),2) and `pay_status` = 1 then 1 else 0 end) as '6'
from ims_integral_order

  2、数据库返回如下:

  3、关键词

//格式化时间戳,返回星期数,注意周日返回值为0
date_format(from_unixtime(pay_time,'%y-%m-%d'),'%w')
//返回当前时间为一年中第几周
week(date_add(curdate(),interval 6 day),2)
//获取指定日期是一年中的第几周
week(date,mode)
//函数向日期添加指定的时间间隔。
date_add(date,interval expr type)
//所以上面的查询条件为星期和第几周同时满足 

影子是一个会撒谎的精灵,它在虚空中流浪和等待被发现之间;在存在与不存在之间….

总结

以上所述是www.887551.com给大家介绍的sql对数据进行按月统计或对数据进行按星期统计的实例代码,希望对大家有所帮助

(0)
上一篇 2022年3月21日
下一篇 2022年3月21日

相关推荐