MySql按周,按月,按日分组统计数据
<!-- 按日查询 -->
SELECT DATE_FORMAT(created_date,'%Y-%m-%d') as time,sum(money) money FROM o_finance_detail where org_id = 1000 GROUP BY time
<!-- 按月查询 -->
SELECT DATE_FORMAT(created_date,'%Y-%m') as time,sum(money) money FROM o_finance_detail where org_id = 1000 GROUP BY time
<!-- 按年查询 -->
SELECT DATE_FORMAT(created_date,'%Y') as time,sum(money) money FROM o_finance_detail where org_id = 1000 GROUP BY time
<!-- 按周查询 -->
SELECT DATE_FORMAT(created_date,'%Y-%u') as time,sum(money) money FROM o_finance_detail where org_id = 1000 GROUP BY time
以下使用的字符串time为自定义名字的字段
-- 今天
select*from time_test where to_days(time)=to_days(now());
-- 昨天
select*from time_test where datediff(time,now())=-1;
-- 本周
select * from time_test where yearweek(date_format(time,'%Y-%m-%d'))=yearweek(now());
-- 上周
select * from time_test where yearweek(date_format(time,'%Y-%m-%d'))=yearweek(now())-1;
-- 本月
select * from time_test where date_format(time,'%Y%m')=date_format(curtime(),'%Y%m') order by time;
-- 上个月
select * from time_test where
period_diff(
date_format(now(),'%Y%m') , date_format(time,'%Y%m')
)=1 order by time
-- 近7天
select * from time_test where date_sub(curdate(),interval 7 day) <=date(time) order by time
-- 近30天
select * from time_test where date_sub(curtime(),interval 30 day) <= date(time) order by time
-- 本季度
select * from time_test where quarter(time)=quarter(now()) order by time
-- 上季度
select * from time_test where quarter(time)=quarter(
date_sub( now(),interval 1 quarter )
)
-- 本年
select * from time_test where year(time)=year(now()) order by time;
-- 去年
select * from time_test where year(time)=year(date_sub(now(),interval 1 year)) order by time;
DATE_FORMAT(date,format)
根据format字符串格式化date值。下列修饰符可以被用在format字符串中:
%M 月名字(January……December)
%W 星期名字(Sunday……Saturday)
%D 有英语前缀的月份的日期(1st, 2nd, 3rd, 等等。)
%Y 年, 数字, 4 位
%y 年, 数字, 2 位
%a 缩写的星期名字(Sun……Sat)
%d 月份中的天数, 数字(00……31)
%e 月份中的天数, 数字(0……31)
%m 月, 数字(01……12)
%c 月, 数字(1……12)
%b 缩写的月份名字(Jan……Dec)
%j 一年中的天数(001……366)
%H 小时(00……23)
%k 小时(0……23)
%h 小时(01……12)
%I 小时(01……12)
%l 小时(1……12)
%i 分钟, 数字(00……59)
%r 时间,12 小时(hh:mm:ss [AP]M)
%T 时间,24 小时(hh:mm:ss)
%S 秒(00……59)
%s 秒(00……59)
%p AM或PM
%w 一个星期中的天数(0=Sunday ……6=Saturday )
%U 星期(0……52), 这里星期天是星期的第一天
%u 星期(0……52), 这里星期一是星期的第一天
%% 一个文字“%”。
统计环比、同比
用已有的案例解释,参考https://blog.csdn.net/pengyangchao/article/details/85990711
有一个业务需求 ,统计各个景区的入园人数、销售额以及入园人数和销售额的同比,以月度为例子同比为例;环比类似
思路分析:表a首先统计出当月的入园人数、销售额,表b统计出同期的入园人数、销售额(在a的基础上减去一年,可),将两个表进行左连接
select
CASE WHEN b.TOTAL_COUNT is NULL OR b.TOTAL_COUNT=0 THEN 0
ELSE ((a.TOTAL_COUNT - b.TOTAL_COUNT)/b.TOTAL_COUNT)*100 //计算入园人数同比
END link_ratio_count,
CASE WHEN b.total_money is NULL OR b.total_money=0 THEN 0
ELSE ((a.total_money - b.total_money)/b.total_money)*100 //计算销售额同比
END link_ratio_money,
a.TOTAL_COUNT as total_count_this,b.TOTAL_COUNT as total_count_last,a.total_money as total_money_this,b.total_money as total_money_last,a.PROJECT_NAME,a.PROJECT_NO
FROM
(SELECT SUM(TOTAL_COUNT) as TOTAL_COUNT,SUM(PAY_MONEY-RETURN_COUNT*o.SELLER_PRICE) as total_money,PROJECT_NAME,o.PROJECT_NO
FROM S_ORDER_C o
LEFT JOIN S_PRODUCT d ON o.PRODUCT_NO=d.PRODUCT_NO
where DateDiff(mm,o.CREATE_TIME,GETDATE()) <= 1// sql server的写法筛选近一月
GROUP BY PROJECT_NAME,o.PROJECT_NO) a //根据景区名称和景区编码分组
LEFT JOIN
(SELECT SUM(TOTAL_COUNT) as TOTAL_COUNT,SUM(PAY_MONEY-RETURN_COUNT*o.SELLER_PRICE) as total_money,PROJECT_NAME
FROM S_ORDER_C o
LEFT JOIN S_PRODUCT d ON o.PRODUCT_NO=d.PRODUCT_NO
where DateDiff(mm,o.CREATE_TIME,'2018-01-01') = 0
GROUP BY PROJECT_NAME) b ON a.PROJECT_NAME = b.PROJECT_NAME