mysql 統計七天數據並分組


業務背景

統計各機型最近7天bug數量來支撐一張圖表:

sql需要查詢最近七天數據並按每天和機型進行分組

思路

1. 查詢最近7天的數據

select * from table where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(column_time);

拓展

查詢最近一天的數據

select * from table where to_days(column_time) = to_days(now());
select * from table where date(column_time) = curdate(); 

查詢最近一個月的數據

select * from table where DATE_SUB(CURDATE(), INTERVAL INTERVAL 1 MONTH) <= date(column_time);

2. 查詢需要的列並分組

例如:

SELECT
	DATE_FORMAT( create_time, '%Y-%m-%d' ) days,
	phone_model ,
	count(*) count 
FROM
( SELECT * FROM trial_feedback_pdm WHERE DATE_SUB( CURDATE( ), INTERVAL 7 DAY ) <= date( create_time) ) as day
GROUP BY
	 days,phone_model;

參考鏈接:

https://blog.csdn.net/sugar_map/article/details/83857449


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM