MYSQL中按照季度、月份等分组

1、按照月份:
select sum(total_amount) as total, date_format(stat_date, '%Y-%m')  from week_report WHERE `stat_date` BETWEEN '2016-11-02' AND '2017-04-30' group by date_format(stat_date, '%Y-%m');
select sum(total_amount) as total,date_format(stat_date, '%Y-%m')   from week_report WHERE `stat_date` BETWEEN '2016-12-11' AND '2016-12-22' group by date_format(stat_date, '%Y-%m');
获得按照月份分组进行汇总的数据。
 
concat()连接字符串
 
-- month
select CONCAT(YEAR(stat_date),'_',DATE_FORMAT(stat_date,'%m')) months ,sum(total_amount) as count_amount, sum(total_new_user) as count_new_user, sum(da_active_user) as count_active_user from xxx
WHERE `stat_date` BETWEEN '2016-01-02' AND '2017-05-30' group by months;
 
-- 季度
select CONCAT(YEAR(stat_date),'_',quarter(stat_date)) qu,sum(total_amount) as count_amount, sum(total_new_user) as count_new_user, sum(da_active_user) as count_active_user from xxx
WHERE `stat_date` BETWEEN '2016-01-02' AND '2017-05-30' group by qu;
 
-- 周
select CONCAT(YEAR(stat_date),'_',DATE_FORMAT(stat_date,'%U')) weeks,sum(total_amount) as count_amount, sum(total_new_user) as count_new_user, sum(da_active_user) as count_active_user from xxx
WHERE `stat_date` BETWEEN '2016-01-02' AND '2017-05-30' group by weeks;
 
-- 天
select CONCAT(YEAR(stat_date),'_',DATE_FORMAT(stat_date,'%m'),'_',DATE_FORMAT(stat_date,'%d')) days, sum(total_amount) as count_amount, sum(total_new_user) as count_new_user, sum(da_active_user) as count_active_user from xxx
WHERE `stat_date` BETWEEN '2016-01-02' AND '2017-05-30' group by days;

--环比  同比
  SELECT CONCAT(yy,"-",IF(mm<10,CONCAT("0",mm),mm)) 日期,项目数量 审计问题数,项目数量/ss3 占比,(项目数量-b.ss2)/b.ss2*100 环比, 
(项目数量-c.ss1)/c.ss1*100 同比
    FROM  (( SELECT  COUNT(b.id) 项目数量,year(finishTime) yy,month(finishTime) mm    FROM       
	 biz_workflow_instance a  LEFT JOIN ic0yb_audit_problem_record  b  on a.bizObjectId=b.bizObjectId 
			 WHERE  a.schemaCode ='project_records' and  a.state = 'COMPLETED'   
GROUP BY yy,mm ) a  LEFT JOIN
( SELECT  COUNT(b.id) ss3, year(finishTime) yy3,month(finishTime) mm3    FROM    biz_workflow_instance a
LEFT JOIN ic0yb_audit_problem_record  b  on a.bizObjectId=b.bizObjectId 
		WHERE  schemaCode ='project_records'  and  (a.state = 'COMPLETED'  or a.state = 'PROCESSING' )
	 GROUP BY yy3,mm3  )  d  on a.yy=d.yy3  and  d.mm3=a.mm
 LEFT JOIN

(select count(b.id) ss2,month(finishTime)mm2 ,year(finishTime) yy2
from biz_workflow_instance a LEFT JOIN ic0yb_audit_problem_record  b  on a.bizObjectId=b.bizObjectId 
 where schemaCode ='project_records' and  state = 'COMPLETED' 
	group by mm2,yy2
)  b   
on (b.yy2 = a.yy and b.mm2+1 = a.mm OR (b.yy2=a.yy-1 
AND b.mm2 = 12 AND a.mm = 1))

left join ( select month(finishTime) mm1,
year(finishTime) yy1,
COUNT(b.id) ss1 from biz_workflow_instance a LEFT JOIN ic0yb_audit_problem_record  b  on a.bizObjectId=b.bizObjectId    WHERE  schemaCode ='project_records' and  state = 'COMPLETED'
GROUP BY mm1,yy1) c
on a.mm = c.mm1 
and a.yy-1 = c.yy1	

你可能感兴趣的:(mysql,mysql,sql,mybatis)