zoukankan      html  css  js  c++  java
  • mysql按日期分组统计数据

    一,最近十二个月:没有数据的月份不展示

    SELECT COUNT(*) AS visitCount, DATE_FORMAT(create_time, '%m') AS month FROM auditlog
            WHERE operate_type = 10 AND DATE_FORMAT(create_time,'%Y-%m')>
            DATE_FORMAT(date_sub(curdate(), interval 12 month),'%Y-%m')
            GROUP BY month;

     二,最近十二个月,没有数据的补0

    SELECT DATE_FORMAT(CURDATE(), '%Y-%m') AS month ,COUNT(id) AS visitCount FROM auditlog WHERE operate_type = 10 AND DATE_FORMAT(CURDATE(), '%Y-%m') = DATE_FORMAT(create_time,'%Y-%m')
            UNION SELECT DATE_FORMAT((CURDATE() - INTERVAL 1 MONTH), '%Y-%m') AS month ,COUNT(id) AS visitCount FROM auditlog WHERE operate_type = 10 AND DATE_FORMAT((CURDATE() - INTERVAL 1 MONTH), '%Y-%m') = DATE_FORMAT(create_time,'%Y-%m')
            UNION SELECT DATE_FORMAT((CURDATE() - INTERVAL 2 MONTH), '%Y-%m') AS month ,COUNT(id) AS visitCount FROM auditlog WHERE operate_type = 10 AND DATE_FORMAT((CURDATE() - INTERVAL 2 MONTH), '%Y-%m') = DATE_FORMAT(create_time,'%Y-%m')
            UNION SELECT DATE_FORMAT((CURDATE() - INTERVAL 3 MONTH), '%Y-%m') AS month ,COUNT(id) AS visitCount FROM auditlog WHERE operate_type = 10 AND DATE_FORMAT((CURDATE() - INTERVAL 3 MONTH), '%Y-%m') = DATE_FORMAT(create_time,'%Y-%m')
            UNION SELECT DATE_FORMAT((CURDATE() - INTERVAL 4 MONTH), '%Y-%m') AS month ,COUNT(id) AS visitCount FROM auditlog WHERE operate_type = 10 AND DATE_FORMAT((CURDATE() - INTERVAL 4 MONTH), '%Y-%m') = DATE_FORMAT(create_time,'%Y-%m')
            UNION SELECT DATE_FORMAT((CURDATE() - INTERVAL 5 MONTH), '%Y-%m') AS month ,COUNT(id) AS visitCount FROM auditlog WHERE operate_type = 10 AND DATE_FORMAT((CURDATE() - INTERVAL 5 MONTH), '%Y-%m') = DATE_FORMAT(create_time,'%Y-%m')
            UNION SELECT DATE_FORMAT((CURDATE() - INTERVAL 6 MONTH), '%Y-%m') AS month ,COUNT(id) AS visitCount FROM auditlog WHERE operate_type = 10 AND DATE_FORMAT((CURDATE() - INTERVAL 6 MONTH), '%Y-%m') = DATE_FORMAT(create_time,'%Y-%m')
            UNION SELECT DATE_FORMAT((CURDATE() - INTERVAL 7 MONTH), '%Y-%m') AS month ,COUNT(id) AS visitCount FROM auditlog WHERE operate_type = 10 AND DATE_FORMAT((CURDATE() - INTERVAL 7 MONTH), '%Y-%m') = DATE_FORMAT(create_time,'%Y-%m')
            UNION SELECT DATE_FORMAT((CURDATE() - INTERVAL 8 MONTH), '%Y-%m') AS month ,COUNT(id) AS visitCount FROM auditlog WHERE operate_type = 10 AND DATE_FORMAT((CURDATE() - INTERVAL 8 MONTH), '%Y-%m') = DATE_FORMAT(create_time,'%Y-%m')
            UNION SELECT DATE_FORMAT((CURDATE() - INTERVAL 9 MONTH), '%Y-%m') AS month ,COUNT(id) AS visitCount FROM auditlog WHERE operate_type = 10 AND DATE_FORMAT((CURDATE() - INTERVAL 9 MONTH), '%Y-%m') = DATE_FORMAT(create_time,'%Y-%m')
            UNION SELECT DATE_FORMAT((CURDATE() - INTERVAL 10 MONTH), '%Y-%m') AS month ,COUNT(id) AS visitCount FROM auditlog WHERE operate_type = 10 AND DATE_FORMAT((CURDATE() - INTERVAL 10 MONTH), '%Y-%m') = DATE_FORMAT(create_time,'%Y-%m')
            UNION SELECT DATE_FORMAT((CURDATE() - INTERVAL 11 MONTH), '%Y-%m') AS month ,COUNT(id) AS visitCount FROM auditlog WHERE operate_type = 10 AND DATE_FORMAT((CURDATE() - INTERVAL 11 MONTH), '%Y-%m') = DATE_FORMAT(create_time,'%Y-%m')

     三,所有数据按月统计,没有数据的月份不展示

    select date_format(create_time, '%m') month, count(*) visitCount
    from auditlog WHERE operate_type = 10
    group by date_format(create_time, '%Y-%m');

  • 相关阅读:
    在子线程中更新ProgressBar为null
    有关ContentProvider及相关一系列的简单用法(持续添加)
    Android内容提供者使用及创建
    Android中关于时间的操作
    Cell的一些坑: UITableViewCell宽度,在iphone5的时候是320,在iphone6的时候为啥也是320?
    处理数据源(根据条目字数多少 ,动态显示一行里有多少个条目,类似天猫搜索历史)
    iOS开发之如何跳到系统设置里的各种设置界面
    Block作为property属性实现页面之间传值(代替Delegate代理与协议结合的方法)
    xcode7的那些坑-“Your binary is not optimized for iPhone 5” (ITMS-90096) when submitting
    PresentViewController切换界面(一些系统自带的页面切换动画)
  • 原文地址:https://www.cnblogs.com/cailijuan/p/11750805.html
Copyright © 2011-2022 走看看