暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

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

原创 大鱼 2021-10-12
824

mysql中关于分组的使用

参考这个使用

  1.  

    select DATE_FORMAT(create_time,'%Y%u') weeks,count(caseid) count from tc_case group by weeks;

  2.  

    select DATE_FORMAT(create_time,'%Y%m%d') days,count(caseid) count from tc_case group by days;

  3.  

    select DATE_FORMAT(create_time,'%Y%m') months,count(caseid) count from tc_case group by months;

  4.  

     

  5.  

    1、按照月份:

  6.  

    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');

  7.  

    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');

  8.  

    获得按照月份分组进行汇总的数据。

  9.  

     

  10.  

    concat()连接字符串

  11.  

     

  12.  

    -- month

  13.  

    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

  14.  

    WHERE `stat_date` BETWEEN '2016-01-02' AND '2017-05-30' group by months;

  15.  

     

  16.  

    -- 季度

  17.  

    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

  18.  

    WHERE `stat_date` BETWEEN '2016-01-02' AND '2017-05-30' group by qu;

  19.  

     

  20.  

    -- 周

  21.  

    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

  22.  

    WHERE `stat_date` BETWEEN '2016-01-02' AND '2017-05-30' group by weeks;

  23.  

     

  24.  

    -- 天

  25.  

    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

  26.  

    WHERE `stat_date` BETWEEN '2016-01-02' AND '2017-05-30' group by days

「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论