mysql中按照季度 月份等分組

2021-08-19 19:18:56 字數 2352 閱讀 1614

mysql中按照季度、月份等分組

2023年08月21日 15:06:12

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 ***

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 ***

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 ***

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 ***

where

`stat_date` between '2016-01-02'

and'2017-05-30'

group

by days;

MySQL 按照年月日周季度分組

mysql按照年月日季度分組,結合日期函式 顯示周在年中第幾周的 返回當前函式所在那個季度 參考文章 將字串時間轉為日期,但是只能到日,想轉成年月形式不行 str to date date,y m d oracle中的to date y 代表4位的年份 y 代表2為的年份 m 代表月,格式為 01 ...

Mysql按照時間分組統計

select update time,count id as count from eemp track group by substring update time,1,10 select date format create time,y m d as days count as count f...

MySql 使用GROUP BY 按照日期分組統計

select date format 時間欄位名,y m d count from table where 條件 group by date for mat 時間欄位名,y m d group by 語句可以根據乙個或多個列對結果集進行分組。結合count 可以進行資料統計。注 在mysql中因為不...