MySQL数据篇(五)--SQL对数据进行按月统计,或对数据进行按星期统计

优惠码优惠 2019-08-13

mysql 函数 SQL

MySQL数据篇(五)--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)
//所以上面的查询条件为星期和第几周同时满足
复制代码
原文地址https://www.cnblogs.com/camg/p/11345086.html

登录 后评论
下一篇
冒顿单于
11164人浏览
2019-08-28
相关推荐
Mysql 常用查询命令
1046人浏览
2016-01-20 15:49:32
MySQL查询
947人浏览
2015-09-17 21:52:00
MySQL查询
701人浏览
2015-09-17 21:52:00
wordpress常用插件汇总
2020人浏览
2017-12-04 22:26:00
数据化管理学习笔记
581人浏览
2017-08-15 14:37:00
Mysql开发规范
518人浏览
2013-11-07 11:21:25
23个MySQL常用查询语句
313人浏览
2015-08-18 17:12:00
0
0
0
119